Laravel 5.3在Auth中更改用户表 [英] Laravel 5.3 Change user table in Auth

查看:71
本文介绍了Laravel 5.3在Auth中更改用户表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Laravel 5.3 and used the make:auth artisan 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 Company table instead of the User table?

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

I already tried to change in the config/auth.php file 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?

Company注册并登录后,它具有邀请Users的能力,因此必须保留原始的User

After the Company registers and logs in, it has the ability to invite Users, therefore the original User table has to remain

推荐答案

Laravel 5.3在Auth实现中进行了更改.对我来说,这种方式解决了这个问题:

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

首先,在数据库中提供符合用于标识的标准的公司表.因此,它需要一个名称,电子邮件,密码和"remember_token"列.可以在此处找到详细信息.

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.

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

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

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

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

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

use Illuminate\Foundation\Auth\User as Authenticatable;

在Company类中,定义可填充和隐藏的字段.

In the Company class, define fillable and hidden fields.

class Company extends Authenticatable {

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

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

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

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

use App\Company;

使用Company :: create

Adjust the create and validator function in the RegisterController.php with Company::create

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',
    ]);
}

'电子邮件'=>'必填|电子邮件|最大:255 |唯一:公司'
(公司模型的表名将是公司)

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

希望这会有所帮助!

这篇关于Laravel 5.3在Auth中更改用户表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆