我目前正在我的应用程序中使用laarvel5.4身份验证;我想更改用户表名,同时保持其在身份验证逻辑中的角色,我只需要更改名称。
似乎Laravel在最新版本中更改了Auth文件和代码结构,因此auth.php并不像以前版本的laravel那样。
到目前为止,我已经完成了以下操作,但它仍然没有工作gy给我一个错误说表用户不存在:
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,
],
],
]。但是,在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',
];
}
您可以在迁移file中更改表名,然后在User.php模型中更改表名变量。
例:
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
你需要改变两个地方
1.在隐藏app / User.php数组后添加此行
protected $hidden = [
'password', 'remember_token',
];
protected $table = 'another_table_name';
2.在RegisterController中,在validator方法中更改表名:
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',
]);
}