Laravel 5.3 在 Auth 中更改用户表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39514118/
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:27:44  来源:igfitidea点击:

Laravel 5.3 Change user table in Auth

laravellaravel-5

提问by Adam Silva

I'm using Laravel 5.3 and used the make:authartisan command to scaffold the login/registration system. I'm doing my login as companies, so I have a table called Company. How do I change the original sql to go get the email and password from the Companytable instead of the Usertable?

我正在使用 Laravel 5.3 并使用make:authartisan 命令来搭建登录/注册系统。我正在以公司身份登录,所以我有一个名为Company. 如何更改原始sql以从Company表而不是User表中获取电子邮件和密码?

I already tried to change in the config/auth.phpfile in the providers part, but when I changed 'model' => App\User::class,to 'model' => App\Company::class,, it started logging in, but regardless if the email and password input were completely wrong. Any ideas?

我已经尝试更改config/auth.php提供程序部分中的文件,但是当我更改'model' => App\User::class,为 时'model' => App\Company::class,,它开始登录,但无论电子邮件和密码输入是否完全错误。有任何想法吗?

EDIT: After the Companyregisters and logs in, it has the ability to invite Users, therefore the original Usertable has to remain

编辑:Company注册和登录后,它有能力邀请Users,因此原始User表必须保留

回答by Koeffi

Laravel 5.3 has changes in the Auth implementation. For me, this way solved it:

Laravel 5.3 对 Auth 实现进行了更改。对我来说,这种方式解决了它:

First, provide a company table in the database that fulfils the criteria to be used for identification. Thus, it needs a name, email, password and remember_token column. Details can be found here.

首先,在数据库中提供一个满足用于识别的标准的公司表。因此,它需要姓名、电子邮件、密码和 remember_token 列。可在此处找到详细信息。

In the config/auth.php change the users model to your company class.

在 config/auth.php 中,将用户模型更改为您的公司类。

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

Create a Company class in the App folder that extends the Auth, so use:

在扩展 Auth 的 App 文件夹中创建一个 Company 类,因此使用:

use Illuminate\Foundation\Auth\User as Authenticatable;

In the Company class, define fillable and hidden fields.

在 Company 类中,定义可填写和隐藏的字段。

class Company extends Authenticatable {

protected $fillable = [
    'name', 'email', 'password',
];

protected $hidden = [
    'password', 'remember_token',
];
}

In the RegisterController.php change "use App\User" to

在 RegisterController.php 中,将“使用 App\User”更改为

use App\Company;

Adjust the createand validatorfunction in the RegisterController.php with Company::create

使用 Company::create调整RegisterController.php 中的createvalidator功能

protected function create(array $data)
{
    return Company::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:companies',
        'password' => 'required|min:6|confirmed',
    ]);
}

'email' => 'required|email|max:255|unique:companies'
(table name for Company Model will be companies)

'email' => 'required|email|max:255|unique: Companies'
(公司模型的表名将是公司)

Hope this helps!

希望这可以帮助!

回答by Elnur Ibrahim-zade

DO NOT FORGET TO CHANGE VALIDATION IN REGISTERCONTOLLER.PHP AS WELL.

不要忘记更改 REGISTERCONTOLLER.PHP 中的验证。

from

'email' => 'required|email|max:255|unique:users',

to

'email' => 'required|email|max:255|unique:company',