Laravel - 如何从单独的字段中获取名字和姓氏,并将它们存储在db中的名称列中?
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 code>,我正在建设中 那个。 问题是它只有“name”字段而不是“firstName”和“lastName”字段。 我在哪里配置这些选项? 我想从名字和姓氏中获取输入,并将它们与它们之间的空格连接起来,然后将它们作为名称存储在我的数据库中。 在哪里可以做到这一点? 我在哪里配置我的选项,如果我想为我的用户添加地址或电话号码? 我研究了很多,我真的很困惑所有这些特性,所以请有人就如何进行用户认证给我一些建议吗? p>
div>
For use name instead of first_name and last_name open database/migration/create_user_table
write
$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
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
According to the documentation, you can modify the AuthController
class in the create method to set which fields are used to create new users.
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.