Laravel - 如何从单独的字段中获取名字和姓氏并将它们存储在 db 的 name 列中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36565613/
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
Laravel - How to take first name and last name from separate fields and store them in name column in db?
提问by Codearts
So I used php artisan make:auth
in my project and I am building on top of that. Problem is it has only "name" field and not "firstName" and "lastName" field. Where do I configure those options? I want to take the input from first name and last name and concatenate them with a space between them, then store them as name in my db. Where is the place to do that? Where do I configure my options like if I want to add an address or phone number for my user? I researched a lot and I am really confused with all those traits so please could someone give me some advice on how to proceed with user authentication?
所以我php artisan make:auth
在我的项目中使用了它,我正在构建它。问题是它只有“姓名”字段,而不是“名字”和“姓氏”字段。我在哪里配置这些选项?我想从名字和姓氏中获取输入并将它们与它们之间的空格连接起来,然后将它们作为名称存储在我的数据库中。哪里有这样做的地方?我在哪里配置我的选项,比如我是否想为我的用户添加地址或电话号码?我研究了很多,我真的对所有这些特征感到困惑,所以请有人给我一些关于如何进行用户身份验证的建议吗?
回答by Imtiaz Pabel
For use name instead of first_name and last_name open database/migration/create_user_table
write
对于使用 name 而不是 first_name 和 last_name open database/migration/create_user_table
write
$table->string('name');
$table->string('name');
instead of
代替
$table->string('first_name');
$table->string('last_name');
After that open app/Http/Controller/Auth/AuthController.php
and then put your code like that
之后打开app/Http/Controller/Auth/AuthController.php
然后像这样放置你的代码
protected function create(array $data)
{
return User::create([
'name' => $data['first_name'].' '.$data['last_name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
if you want to use more fields,write those fields in migration file which is on database/migration and then use those fields in resources/views/auth/register.blade.php
that files's html form,and after that change AuthController.php
file
如果您想使用更多字段,请在数据库/迁移上的迁移文件中写入这些字段,然后在resources/views/auth/register.blade.php
该文件的 html 表单中使用这些字段,然后更改AuthController.php
文件
回答by Dexter
According to the documentation, you can modify the AuthController
class in the create method to set which fields are used to create new users.
根据文档,可以修改AuthController
create方法中的类来设置哪些字段用于创建新用户。
However, I'd like to ask you to reconsider requiring both fields. Is it really necessary? Does your design spec require it? W3C recommends a single "name" field for better user experience. I understand that some apps will require both, for things like human resources and accounting purposes, but this is worth considering. General consumer type users desire easier forms, so ask as little as possible of your user.
但是,我想请您重新考虑要求这两个字段。真的有必要吗?您的设计规范需要它吗?W3C 建议使用单个“名称”字段以获得更好的用户体验。我知道有些应用程序需要两者,例如人力资源和会计目的,但这值得考虑。一般消费者类型的用户需要更简单的形式,所以尽可能少问你的用户。