styles9
styles9

Reputation: 23

How to edit the auth in laravel?

I want to create a registration form that includes username, Phone number and an address in the registration process. How do you edit the auth to add or delete fields for registration and login?

Upvotes: 1

Views: 3568

Answers (3)

Mortada Jafar
Mortada Jafar

Reputation: 3679

first open RegisterController class to make some modifications

pathToYourApp/app/Http/Controllers/Auth/RegisterController.php

1- add your extra fields to validator method

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'phone' => 'required|numeric',
         // add another extra fields here
    ]);
}

2- now make this fields added to create statement by add extra fields to create method

protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'phone' => $data['phone'],
             // add another extra fields here
        ]);
    }

of course you should add your extra fields to your user migration and add input fields in blade view

Upvotes: 0

Braj
Braj

Reputation: 1

1 update users table using migration

2 run migration

3 made changes in registration view

4 made changes in Auth Registration controller. In validation function and in register function.

Upvotes: 0

ndrwnaguib
ndrwnaguib

Reputation: 6115

As far as I understood your question, to delete/add fields correctly in Laravel's registration form, you will have to modify three different places:

  • Database Migration of users which is placed in /database/migrations/
  • User's model which is placed in /app/User.php
  • Registration controller which is placed in /app/Http/Controllers/Auth/RegisterController.php

    1. You will edit the database migration to add/reomve fields from user's table.
    2. You will edit the fillable array in user's model and add/remove the fields you need (Make sure you type the exact name you have written in the migration file.)
    3. You will have to edit a couple of functions in the registration controller:
      • function validator to validate the fields you receive from the blade file.
      • function create to add/remove the fields you have added/removed in the previous files.

And you of course add the fields in the blade file so can send them through the form.

Upvotes: 2

Related Questions