laravel 5.4 更改认证用户表名 [英] laravel 5.4 change authentication users table name

查看:27
本文介绍了laravel 5.4 更改认证用户表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的应用程序中使用 laarvel5.4 身份验证;并且我想更改用户表名称,同时保持其在身份验证逻辑中的作用,我只需要更改名称即可.

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.

Laravel 似乎在最新版本中更改了 Auth 文件和代码结构,因此 auth.php 看起来不像以前的 Laravel 版本.

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.

到目前为止,我已经完成了以下操作,但它仍然无法正常工作 gy 给我一个错误,指出表用户不存在:

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- 我已经更改了 migrationup()down() 函数来创建并删除 staff 表而不是 users 并成功运行迁移.
  • 2- 我已经更改了 RegisterController 中的 validator() 函数.

  • 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- 我已将 config/auth.php 中的所有 'users' 更改为 'staff',如代码所示:

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' => AppUser::class,
    ],

    // 'staff' => [
    //     'driver' => 'database',
    //     'table' => 'staff',
    // ],
],
'passwords' => [
    'staff' => [
        'provider' => 'staff',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

];

但是,在 app/User.php 我不知道要更改什么,因为在以前的版本中曾经有一个 table 变量,您需要更改它从 users 到新表名的价值,但在我的班级中我没有这样的东西

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 IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
    use Notifiable;
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}

推荐答案

可以在迁移中更改表名 file 然后更改User.php 模型.

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

示例:

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

https://laravel.com/docs/5.4/eloquent#eloquent-模型约定

这篇关于laravel 5.4 更改认证用户表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆