How can I pass variable to register view?
You can achieve that in two ways.
Overriding RegistersUsers
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.
Modifying Routes
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
...