【问题标题】:laravel 7 change authentication users table name and modellaravel 7 更改身份验证用户表名和模型
【发布时间】:2020-11-25 02:05:18
【问题描述】:

我目前在我的应用程序中使用 Laravel 7 身份验证;我想更改用户表名称,同时保持其在身份验证逻辑中的角色。因为我们的主应用程序中已经有一个用户表,所以我们在另一个仪表板中工作,新应用程序的用户表将使冲突。

所以请提供任何解决方案!

【问题讨论】:

  • 您可以将其更改为您想要的任何型号
  • 这是基于 ID 的工作,所以除了 ID 之外的任何内容都可以更改
  • 你可以有一个不同的模型,比如 DashboardUser 和对应的表dashboard_users。然后您需要使用相应的值更新config\auth.php。假设应用只有一张表供用户使用

标签: php laravel laravel-7


【解决方案1】:

假设您将模型命名为 DashboardUser 和表 dashboard_users

然后更新config\auth.php中的值


return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'dashboard_users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'dashboard_users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'dashboard_users',
            'hash' => false,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\DashboardUser::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'dashboard_users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

【讨论】:

    【解决方案2】:

    首先转到您想成为您的身份验证模型的模型(在示例中,Dashboard 是您的身份验证模型,所以转到app/Models/Dashboard.php)然后从Authenticatable 扩展模型。

    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class Dashboard extends Authenticatable
    {
        use Notifiable;
        protected $hidden = [
            'password', 'remember_token',
        ];
    
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];
    }
    

    转到config/auth.php 文件并将默认表(用户)更改为您想要的新表(在示例中为名为@9​​87654326@ 的新表)。

    return [ 
        'defaults' => [
            'guard' => 'web',
            'passwords' => 'dashboards',
        ],
    
        'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'dashboards',
            ],
    
            'api' => [
                'driver' => 'token',
                'provider' => 'dashboards',
            ],
        ],
    
        'providers' => [
            'users' => [
                'driver' => 'eloquent',
                'model' => App\Models\Dashboard::class,
            ],
    
        ],
    
        'passwords' => [
            'users' => [
                'provider' => 'dashboards',
                'table' => 'password_resets',
                'expire' => 60,
            ],
        ],
    
    ];
    

    也别忘了换password_resetstable

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 2017-10-23
      • 2018-09-15
      • 1970-01-01
      相关资源
      最近更新 更多