14

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?

4 Answers 4

27

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.

9

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...

0

This function should be created manually in the RegisterController.php in theapp/http/controllers/auth/ folder. From this controller you can create any function but watch out for duplicate function used by the trait RegistersUsers.php.

Your function could also be written like that:

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

Hope it helps!

0

write showLoginForm() function in your RegisterController.php file, function name is showLoginForm()and it will overwrite laravel default showLoginForm() function.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.