laravel 5.4 更改认证用户表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44449076/
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.4 change authentication users table name
提问by Alladin
I'm currently using the laarvel5.4authentication in my application; and I want to change the users table name while keeping its role as it is in the authentication logic, all I need is just to change the name.
我目前在我的应用程序中使用laarvel5.4身份验证;并且我想更改用户表名称,同时保持其在身份验证逻辑中的作用,我只需要更改名称即可。
It seems that Laravel changer the Auth file and code structure in the latest version, so auth.php doesn't really look as in the previous versions of laravel.
似乎 Laravel 在最新版本中更改了 Auth 文件和代码结构,因此 auth.php 看起来并不像以前版本的 Laravel 那样。
I have done the following so far, but it's still not working gy giving me an error saying that the table users doesn't exist:
到目前为止,我已经完成了以下操作,但它仍然无法正常工作 gy 给我一个错误,说表用户不存在:
- 1-I have changed the migration's up()and down()functions to create and drop stafftable instead of usersand run the migration successfully.
2-I have changed the validator()function in RegisterController.
3-I have changed all the 'users'to 'staff'in config/auth.php, as shown in the code:
return [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'staff', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'staff', ], 'api' => [ 'driver' => 'token', 'provider' => 'staff', ], ], 'providers' => [ 'staff' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], // 'staff' => [ // 'driver' => 'database', // 'table' => 'staff', // ], ], 'passwords' => [ 'staff' => [ 'provider' => 'staff', 'table' => 'password_resets', 'expire' => 60, ], ],
];
- 1-我已经更改了迁移的up()和down()函数来创建和删除员工表而不是用户并成功运行迁移。
2-我更改了RegisterController 中的validator()函数。
3-我已经改变的所有“用户”到“人员”在配置/ auth.php,如图中的代码:
return [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'staff', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'staff', ], 'api' => [ 'driver' => 'token', 'provider' => 'staff', ], ], 'providers' => [ 'staff' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], // 'staff' => [ // 'driver' => 'database', // 'table' => 'staff', // ], ], 'passwords' => [ 'staff' => [ 'provider' => 'staff', 'table' => 'password_resets', 'expire' => 60, ], ],
];
However, in app/User.phpI don't know what to change since in the previous versions there used to be a tablevariable which u need to change its value from usersto the new table name but in my class I don't have such thing
但是,在app/User.php 中我不知道要更改什么,因为在以前的版本中曾经有一个表变量,您需要将其值从用户更改为新表名,但在我的班级中我没有有这样的事
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
回答by jeremykenedy
You can change the table name in the migration fileand then change the table name variable in the User.phpmodel.
您可以更改迁移文件中的表名,然后更改User.php模型中的表名变量。
Example:
例子:
class Flight extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'my_flights';
}
https://laravel.com/docs/5.4/eloquent#eloquent-model-conventions
https://laravel.com/docs/5.4/eloquent#eloquent-model-conventions
回答by Robbani
You need just change in two places
你只需要改变两个地方
1.add this line after hidden array of app/User.php
1.在隐藏的app/User.php数组后面添加这一行
protected $hidden = [
'password', 'remember_token',
];
protected $table = 'another_table_name';
2.In the RegisterController change the table name in the validator method:
2.在 RegisterController 中更改验证器方法中的表名:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:another_table_name',
'password' => 'required|string|min:6|confirmed',
]);
}