Added new column in laravel users table

32 Views Asked by At

Added new column in laravel users table

What is the correct way to add a new column in the Laravel users table? what I do, first make a command in the terminal

 make:migration add_role_to_users_table --table=users

then add a new column in the migration file that has been created,

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->integer('role');
    });
}

And

php artisan migrate

In the controller, I have also inserted new columns for 2 functions

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:8', 'confirmed'],
        'role' => ['required', 'integer', 'min:8', 'confirmed'],
    ]);
}

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'role' => $data['role'],
    ]);
}

and in the model I have also added a new column

protected $fillable = [
    'name',
    'email',
    'password',
    'role'
];

However, nothing happened after I registered, and the new user was not entered into the users table in the database

enter image description here

1

There are 1 best solutions below

0
Irfan Chaniago On

It appears that the error is was because I added a new column in the validator function, which doesn't need to be validated.