如何更改 laravel 中的用户模型以使用除用户之外的其他 table 进行身份验证?

How to change the user model in laravel to use other table for authentication than users?

如何更改默认用户模型以使用其他数据库 table 而不是 laravel 5.3 中的用户 table,正在添加

protected $table = 'my_table_name';

我会做所有事情,还是我必须更换供应商和所有东西。

我不想使用管理员、客户等多重身份验证,我只想要一种身份验证模型,而不是 table "users"。

我正在准备一个应用程序,它有多种类型的用户,如管理员、占星家和用户。它们都有不同的属性,所以我不能使用带有访问控制的单一模型,所以我决定将应用程序分成 3 个网站以使用单一数据库并将它们托管在

这样的子域中
admin.mywebsite.com
astrologer.mywebsite.com
www.mywebsite.com

早些时候我使用的是 multiauth/hesto 插件,但由于某些奇怪的原因,它没有在登录和注册后将我重定向到预期的 url。任何帮助将不胜感激。

您可以使用模型中的 $connection 属性指定要使用的数据库:

class MyModel extends Eloquent
{
    protected $connection = 'another-database-connection';
}

我不确定这是否是您要查找的内容。也就是说,如果您想对多个子域使用相同的 Laravel 应用程序,我建议您查看说明如何在路由中使用子域的文档:

https://laravel.com/docs/5.3/routing#route-group-sub-domain-routing

这将允许您拥有一个 User class 和一个应用程序,但对于您的 3 个子域中的每一个都有特定的路由。

转到您的 config\auth.php

修改配置

'guards' => [
    'astrologer' =>[
        'driver' => 'session',
        'provider' => 'astrologer',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],  

'providers' => [
    'astrologer' => [
        'driver' => 'eloquent',
        'model' => App\Astrologer::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],

查看此处的答案了解更多详情:

勾选这个Using Different Table for Auth in Laravel

编辑 app/config/auth.php 以更改 table.

'table' => 'yourTable'
'model' => 'YourModel',

那么你的模特:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class YourModel extends Eloquent implements UserInterface, RemindableInterface 
{
    use UserTrait;
    use RemindableTrait;

    protected $table = 'your_table';
    protected $fillable = array('UserName', 'Password');    
    public $timestamps = true;
    public static $rules = array();    
}

转到config/auth。php 在这种情况下更改提供者 我将 api 提供者从用户更改为 custom_user,然后使用 custom_user 模型 class

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver'=>'passport',
        'provider'=>'custom_user'
    ],
]



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