【问题标题】:How to use custom table as auth table instead user table?如何使用自定义表作为身份验证表而不是用户表?
【发布时间】:2016-11-26 02:01:53
【问题描述】:

我是 Laravel 5.3 的初学者。 我想要的是使用自定义表作为身份验证表而不是用户表。 因为用户表应该用于其他目的。 如何编辑 config/auth.php 或其他内容? 请帮帮我!

文件 auth.php

<?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' => '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' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | 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\Admin::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' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

文件 Admin.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\Admin as Authenticatable;

class Admin extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

【问题讨论】:

    标签: authentication laravel-5 tablename


    【解决方案1】:

    在您的 auth.php 中将 providers.users.model 更改为您的自定义类。

    喜欢:

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

    Docs

    更新

    在您的Admin.php 文件中更改

    use Illuminate\Foundation\Auth\Admin as Authenticatable;
    

    use Illuminate\Foundation\Auth\User as Authenticatable;
    

    【讨论】:

    • 抱歉,它不能正常工作,出现错误“找不到 Class 'Illuminate\Foundation\Auth\Admin'”
    • 你的自定义表名是admin吗?如果是,那么您能否在问题中显示更新后的 auth.phpAdmin.php 文件。
    • 哦,那个错误消失了,但他们说我必须在创建管理表之前创建管理表。我必须重命名它吗?为什么 laravel 认为 admins 表是 auth 表而不是 admin 表? “s”后缀呢?
    • 按照约定,“snake case”,类的复数名称将用作表名,除非明确指定另一个名称。遵循约定非常好,因此您可以将表名更改为admins。否则,您可以阅读文档here 来更改表名。
    • 很高兴为您提供帮助!您应该彻底阅读文档,从长远来看,它将对您有很大帮助。如果可能,请为答案投票,这有助于我回答更多问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2011-07-14
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多