laravel 5.4 change authentication users table name

此生再无相见时 提交于 2019-11-29 09:54:53

问题


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

回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/44449076/laravel-5-4-change-authentication-users-table-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!