Laravel开箱即用地创建了很多东西,但是在某些方面却遗漏了很多东西。我说的是身份验证,在框架中创建它仍然太复杂了。
我仍然梦想着在工匠中创建空白身份验证的命令,在该命令中我可以使用所需的所有字段进行自定义,而不依赖于预定义的字段。但是,既然我们还没有,那就让我们为我们现在拥有的解决吧。
嗯,自从我尝试完全可定制的身份验证以来已经过了两天了,但这确实是一个挑战。我的问题是当前将默认字段“密码”更改为其他名称,例如“ senha”。
为此,我遵循了文档(Eloquent: Mutators)中的说明。我在模型上创建了一个访问器,其中新的用户表在哪里,我称之为“ cliente”。
所以,我的模型是:
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Cliente extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'nome', 'login', 'senha',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'senha', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
//protected $casts = [
// 'email_verified_at' => 'datetime',
//];
public function getAuthPassword()
{
return $this->senha;
}
protected $table = 'clientes';
protected $guard = 'clientes';
}
有趣的是,访问器仅适用于标准用户模型,而不适用于其他模型。