[英]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注册.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.假设您有一个名为RegisterController
的控制器, create
方法应该只返回view
及其相应的数据。
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:你还应该有一个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
}
and your routes/web.php
file should contain the following routes:并且您的routes/web.php
文件应包含以下路由:
Route::get('register', 'RegisterController@create');
Route::post('register', 'RegisterController@store');
This is really basics of Laravel, please read the docs .这真的是 Laravel 的基础知识,请阅读文档。 PS Laravel comes with great Authentication system, which you can override to your needs. PS Laravel 带有很棒的身份验证系统,您可以根据需要覆盖它。
This is what I did in Laravel 7, I'm also using the default authentication implementation from Laravel, so it might help这就是我在 Laravel 7 中所做的,我也使用了 Laravel 的默认身份验证实现,所以它可能会有所帮助
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.解决方案是:在RegisterController
覆盖showRegistrationForm
方法(在/vendor/laravel/ui/auth-backend/RegisterUsers
)并添加数据数组。
So you will add something like this to your RegisterController
因此,您将在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.