I have used php artisan make:auth
it generates the register view and /register route. But I need to pass a variable to this register view:
<label>Region:</label>
<select name="region" id="region" class="form-control" >
<option>--Select a Region--</option>
@foreach($region as $reg)
<option value="{{$reg->region_id}}">{{$reg->region_name}}</option>
@endforeach
</select><br>
Something like:
public function register()
{
$region=Region::all();
return view('auth.register')->with('region',$region);
}
But where is this kind of method?
You can achieve that in two ways.
Laravel's default auth uses RegistersUsers trait on RegisterController to render view. What you can do is simply override the function found on Illuminate\Foundation\Auth\RegistersUsers on RegisterController like the following
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
$region=Region::all();
return view('auth.register', compact('region'));
}
Now above code will over ride the trait and use the showRegistrationForm from controller.
When you do php artisan make:auth
, it will add the Auth::routes()
to your web.php file. Remove that and add the following,
// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
Now on route for register, modify the RegisterController@showRegistrationForm to RegisterController@register.
But do not use simply register. Instead use like getRegisterForm. Because register function handles the post registration logic.
In Laravel Auth:
it uses showRegistrationForm()
method to create registration page. So we need to override that method.
Just Override the showRegistrationForm()
method
public function showRegistrationForm() {
$data = [1, 2, 3, 4];
return view ('auth.register')->withData($data);
}
And capture that data in registration page using $data
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With