How to pass a variable to a register:

I used php artisan make:auth , which generates a register lookup route and / register. 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> 

Sort of:

 public function register() { $region=Region::all(); return view('auth.register')->with('region',$region); } 

But where is this method?

+5
source share
4 answers

You can achieve this in two ways.

Register Overrides

Laravel's default auth uses the RegistersUsers function for RegisterController for rendering. What you can do is simply override the function found in Illuminate \ Foundation \ Auth \ RegistersUsers on RegisterController, as shown below.

 /** * Show the application registration form. * * @return \Illuminate\Http\Response */ public function showRegistrationForm() { $region=Region::all(); return view('auth.register', compact('region')); } 

Now the line will pass over the code and use showRegistrationForm from the controller.

Change Routes

When you do php artisan make:auth , it will add Auth::routes() to your web.php file. Remove this 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 the way to the register, change RegisterController @showRegistrationForm to RegisterController @register.

But do not use just case. Use as getRegisterForm instead. Because the register function handles the logging logic of the record.

+5
source

In Laravel Auth: it uses the showRegistrationForm() method to create a login page. Therefore, we need to override this method.

Just override the showRegistrationForm() method

 public function showRegistrationForm() { $data = [1, 2, 3, 4]; return view ('auth.register')->withData($data); } 

And write this data on the registration page using $data ...

+1
source

This function must be manually created in RegisterController.php in the app/http/controllers/auth/ folder. You can create any function from this controller, but watch out for the repeating function used by the RegistersUsers.php attribute.

Your function can also be written as follows:

 public function register() { $region = Region::all(); return view('auth.register', compact('region')); // This will send the $region variable to the view } 

Hope this helps!

0
source

write the showLoginForm () function in the RegisterController.php file, the name of the showLoginForm() function and will overwrite the showLoginForm () function by default with laravel.

 public function showLoginForm() { $region=Region::all(); return view('auth.register')->with('region',$region); } 
0
source

Source: https://habr.com/ru/post/1264429/


All Articles