簡體   English   中英

Laravel 5.4 注冊表中的模型綁定

[英]Model binding in registration form for Laravel 5.4

我正在嘗試從數據庫中提取數據列表並將其顯示為注冊表單中的下拉列表。 但是我收到錯誤未定義的變量大學。

注冊控制器

/**
     * 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'));
    }

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

我收到一個錯誤大學未定義。

假設您有一個名為RegisterController的控制器, create方法應該只返回view及其相應的數據。

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

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

你還應該有一個store方法:

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

}

並且您的routes/web.php文件應包含以下路由:

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

這真的是 Laravel 的基礎知識,請閱讀文檔 PS Laravel 帶有很棒的身份驗證系統,您可以根據需要覆蓋它。

這就是我在 Laravel 7 中所做的,我也使用了 Laravel 的默認身份驗證實現,所以它可能會有所幫助

解決方案是:在RegisterController覆蓋showRegistrationForm方法(在/vendor/laravel/ui/auth-backend/RegisterUsers )並添加數據數組。

因此,您將在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
            ]
        );
    }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM