在 laravel 身份验证中设置自定义用户详细信息



我有 smf 论坛,在 db 中使用smf_members表。有这样的字段:

array(32) {
["groups"]=>
array(2) {
[0]=>
int(1)
[1]=>
int(25)
}
["possibly_robot"]=>
bool(false)
["id"]=>
string(2) "28"
["username"]=>
string(7) "Milan95"
["name"]=>
string(7) "Milan95"
["email"]=>
string(16) "******"
["passwd"]=>
string(40) "******"
["language"]=>
string(18) "serbian_latin-utf8"
["is_guest"]=>
&bool(false)
["is_admin"]=>
&bool(true)
["theme"]=>
string(1) "7"
["last_login"]=>
string(10) "1576930811"
}

我也有拉拉维尔模型"用户"。

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
use SpatiePermissionTraitsHasRoles;
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
protected $fillable = [
'real_name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];

protected $table = 'smf_members';
protected $primaryKey = 'id_member';
public $timestamps = false;
}

但我只能在致电时访问该信息: 用户::查找($id(;然后是来自smf_members的数据。

我找不到任何方法将活动会话和数据放入用户模型和字段,从

global $user_info;
var_dump($user_info);

我从那里的第一个"代码"获取数据的地方。

谢谢,请帮助:)

您可以使用雄辩的访问器。将模型更新为:

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
use SpatiePermissionTraitsHasRoles;
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
protected $fillable = [
'real_name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
// add this line if you're using api
protected $appends = ['smf_info'];

protected $table = 'smf_members';
protected $primaryKey = 'id_member';
public $timestamps = false;
// add this block
public function getSmfInfoAttribute()
{
return // your logic to find current user smf info
}
}

然后,您可以通过以下方式访问该属性User::find($id)->smf_info

相关内容

  • 没有找到相关文章

最新更新