Alladin
Alladin

Reputation: 1072

laravel 5.4 change authentication users table name

I'm currently using the laarvel5.4 authentication in my application; and I want to change the users table name while keeping its role as it is in the authentication logic, all I need is just to change the name.

It seems that Laravel changer the Auth file and code structure in the latest version, so auth.php doesn't really look as in the previous versions of laravel.

I have done the following so far, but it's still not working gy giving me an error saying that the table users doesn't exist:

However, in app/User.php I don't know what to change since in the previous versions there used to be a table variable which u need to change its value from users to the new table name but in my class I don't have such thing

<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    use Notifiable;
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Upvotes: 7

Views: 35625

Answers (2)

Robbani
Robbani

Reputation: 151

You need just change in two places

1.add this line after hidden array of app/User.php

 protected $hidden = [
    'password', 'remember_token',
];

protected $table = 'another_table_name';

2.In the RegisterController change the table name in the validator method:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:another_table_name',
        'password' => 'required|string|min:6|confirmed',
    ]);
}

Upvotes: 15

jeremykenedy
jeremykenedy

Reputation: 4285

You can change the table name in the migration file and then change the table name variable in the User.php model.

Example:

class Flight extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'my_flights';
}

https://laravel.com/docs/5.4/eloquent#eloquent-model-conventions

Upvotes: 16

Related Questions