向 Laravel 5 身份验证添加字段不会插入到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28392436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding fields to Laravel 5 authentication don't get inserted into the database
提问by Noman Ur Rehman
I've been playing around with Laravel 5's out-of-the-box authentication. When I add fields to the schema and migrate, and then I make them fillable - those added fields don't get inserted into the database, the default fields get inserted but the new ones are blank. The form has been updated to include those new fields.
我一直在玩弄 Laravel 5 的开箱即用身份验证。当我将字段添加到架构并迁移,然后使它们可填充时 - 这些添加的字段不会插入到数据库中,默认字段会被插入,但新字段是空白的。表单已更新以包含这些新字段。
The console also shows that the form data is being sent correctly.
控制台还显示表单数据正在正确发送。
Am I missing something?
我错过了什么吗?
回答by Noman Ur Rehman
If you look at the file app/Services/Registrar.php
, you will find a create
function in there:
如果您查看文件app/Services/Registrar.php
,您会create
在其中找到一个函数:
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
You need to modify this function to include your form fields. They will be passed in the $data
array.
您需要修改此函数以包含您的表单域。它们将在$data
数组中传递。
There is also a validator
function in that file to apply validation rules to your fields.
validator
该文件中还有一个函数可以将验证规则应用于您的字段。
Also, there is a protected $fillable
array in the app/User.php
model file. You will need to add your custom field names to this array so that they are mass-assignable.
此外,模型文件中有一个protected $fillable
数组app/User.php
。您需要将您的自定义字段名称添加到此数组中,以便它们可以批量分配。
protected $fillable = ['name', 'email', 'password'];
Edited July 9, 2015
2015 年 7 月 9 日编辑
This is applicable to v5.0.1
. The folder structure has changed going forward. The settings found in the app/Services/Registrar.php
file have been moved to app/Http/Controllers/Auth/AuthController.php
这适用于v5.0.1
. 文件夹结构在未来发生了变化。在app/Services/Registrar.php
文件中找到的设置已移至app/Http/Controllers/Auth/AuthController.php