codearts
codearts

Reputation: 2956

Laravel - How to take first name and last name from separate fields and store them in name column in 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?

Upvotes: 3

Views: 12376

Answers (2)

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5445

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

Upvotes: 4

Dexter
Dexter

Reputation: 815

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.

Upvotes: 1

Related Questions