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
Laravel 5.3 Change user table in Auth
提问by Adam Silva
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?
我正在使用 Laravel 5.3 并使用make:auth
artisan 命令来搭建登录/注册系统。我正在以公司身份登录,所以我有一个名为Company
. 如何更改原始sql以从Company
表而不是User
表中获取电子邮件和密码?
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?
我已经尝试更改config/auth.php
提供程序部分中的文件,但是当我更改'model' => App\User::class,
为 时'model' => App\Company::class,
,它开始登录,但无论电子邮件和密码输入是否完全错误。有任何想法吗?
EDIT: After the Company
registers and logs in, it has the ability to invite Users
, therefore the original User
table 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 中的create和validator功能
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',