laravel - 在 laravel auth 中设置自定义用户详细信息
问题描述
我有 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"
}
我也有 laravel 模型“用户”。
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
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;
}
但是我只能在调用时访问该信息: User::find($id); 然后有来自 smf_members 的数据。
我找不到任何方法将活动会话和数据放入用户模型和字段,从
global $user_info;
var_dump($user_info);
我从那里的第一个“代码”获取数据的地方。
谢谢,请帮忙:)
解决方案
您可以使用 eloquent 访问器。将您的模型更新为:
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
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
推荐阅读
- reactjs - 如何在静态函数 React native 中设置状态
- canvas - 缩放后paperjs添加点的问题
- javascript - 如何从 javaScript 对象中删除值?
- assembly - ARM 汇编,操作变量
- database - 创建用于连接 Power BI 的平面数据库
- javascript - 悬停显示子元素,失去焦点时隐藏
- c# - Winforms 静态 HandleCreated 或 OnLoad 事件
- mathematical-optimization - 如何在 CPLEX 中使用决策变量进行 if 语句
- postgresql - 如何获取整个数据库存储过程脚本
- sql - 如何更改表以添加约束?