laravel 如何更改laravel中的用户模型以使用其他表进行身份验证而不是用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41014735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:53:40  来源:igfitidea点击:

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

laravellaravel-5.3

提问by Taj Khan

How can i change the default user model to use other database table instead of users table in laravel 5.3, is adding

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

protected $table = 'my_table_name';

will do everything or do i have to change the providers and all.

会做一切还是我必须改变提供者和所有。

I dont wish to use multiple authentication like admin, customers etc i just want one authentication model but not with the table "users".

我不希望使用多个身份验证,如管理员、客户等,我只想要一个身份验证模型,而不是“用户”表。

I am preparing an application which has multiple type of users like admin, astrologers and users. all of them have different attributes so i cant use single model with access control in it so i have decided to divide the application into 3 websites to use single database and host them in subdomains like

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

admin.mywebsite.com
astrologer.mywebsite.com
www.mywebsite.com

earlier i was using multiauth/hesto plugin but for some strange reason it's not redirecting me to the intended url after login and registration. any help will be appreciated.

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

回答by ashanrupasinghe

check this one Using Different Table for Auth in Laravel

检查这个在 Laravel 中使用不同的表进行身份验证

Edit app/config/auth.php to change the table.

编辑 app/config/auth.php 以更改表。

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

then your model:

那么你的模型:

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();    
}

回答by AntoineB

You can specify which database to use using the $connectionattribute in your model:

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

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

I'm not sure it's what you're looking for tho. That said, if you want to use the same Laravel application for multiple subdomains, I'd recommend checking the documentation which explains how to use subdomains in your routes:

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

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

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

This would allow you to have a single Userclass and a single application but have specific routes for each of your 3 subdomains.

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

回答by aimme

go to your config\auth.php

转到您的 config\auth.php

modify the configuration

修改配置

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

See answer here for more details: Can anyone explain Laravel 5.2 Multi Auth with example

有关更多详细信息,请参阅此处的答案:谁能用示例解释 Laravel 5.2 多重身份验证

回答by Aman

Go to config/auth.php Change the provider for this case I changed the api provider from user to custom_user and then use custom_user model class

转到 config/auth.php 更改此案例的提供程序我将 api 提供程序从 user 更改为 custom_user 然后使用 custom_user 模型类

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