简体   繁体   中英

Model binding in registration form for Laravel 5.4

I am trying to pull list of data from database and show it as a dropdown in a registration form. But I get error undefined variable universities.

Register Controller

/**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
*/
    protected function create(array $data)
    {
        $universities = University::lists('id', 'university');
        $sch_depts   = Department::lists('id', 'department');

        return User::create([
            'firstname' => $data['firstname'],
            'lastname' => $data['lastname'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'university' => $data['university'],
            'school_dept' => $data['school_dept'],
        ])
        ->with(compact('universities','sch_depts'));
    }

register.blade.php

<div class="form-group{{ $errors->has('university') ? ' has-error' : '' }}">
                            <label for="university" class="col-md-4 control-label">University</label>

                            <div class="col-md-6">

                                {!! Form::select('university', $universities, null, array('id' => 'universitiy', 'class' => 'form-control')) !!}

                                @if ($errors->has('university'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('university') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

I am getting an error universities undefined.

Let's assume you have a controller called RegisterController , the create method should only return view and it's corresponding data.

public function create() 
{
    $universities = University::lists('id', 'university');
    $sch_depts   = Department::lists('id', 'department');

    return view('register', compact('universities', 'sch_depts'));
}

and you should also have a store method:

public function store(\Illuminate\Http\Request $request)
{

    // your validation goes here
    // or just use form request validation
    // docs: https://laravel.com/docs/5.4/validation#form-request-validation

    User::create([
        'firstname' => $request->get('firstname'),
        'lastname' => $request->get('lastname'),
        'email' => $request->get('email'),
        'password' => bcrypt($request->get('password')),
        'university' => $request->get('university'),
        'school_dept' => $request->get('school_dept'),
    ]);

   // login user here or redirect to success page

}

and your routes/web.php file should contain the following routes:

Route::get('register', 'RegisterController@create');
Route::post('register', 'RegisterController@store');

This is really basics of Laravel, please read the docs . PS Laravel comes with great Authentication system, which you can override to your needs.

This is what I did in Laravel 7, I'm also using the default authentication implementation from Laravel, so it might help

The solution is: in the RegisterController overwrite the method showRegistrationForm (which is declared in /vendor/laravel/ui/auth-backend/RegisterUsers ) and add the data array.

So you will add something like this to your RegisterController

    public function showRegistrationForm()
    {
        $universities = University::lists('id', 'university');
        $sch_depts   = Department::lists('id', 'department');
        
        return view(
            'auth.register',
            [
                'universities' => $universities,
                'sch_depts' => $sch_depts
            ]
        );
    }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM