6
votes

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:

  • 1- I have changed the migration's up() and down() functions to create and drop staff table instead of users and run the migration successfully.
  • 2- I have changed the validator() function in RegisterController.

  • 3- I have changed all the 'users' to 'staff' in config/auth.php, as shown in the code:

     return [
    
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'staff',
    ],
    
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'staff',
        ],
    
        'api' => [
            'driver' => 'token',
            'provider' => 'staff',
        ],
    ],
    
    'providers' => [
        'staff' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    
        // 'staff' => [
        //     'driver' => 'database',
        //     'table' => 'staff',
        // ],
    ],
    'passwords' => [
        'staff' => [
            'provider' => 'staff',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
    

    ];

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',
    ];
}
2

2 Answers

16
votes

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

14
votes

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',
    ]);
}