我有一个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模型“ User”。
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;
}
但是我只能在致电时访问该信息: 用户:: find($ id);那么就有来自smf_members的数据。
我找不到从
将活动会话和数据放入用户模型和字段的任何方法 global $user_info;
var_dump($user_info);
我从那里的第一个“代码”那里获取数据。
谢谢,请帮助:)
答案 0 :(得分:1)
您可以使用雄辩的访问器。将模型更新为:
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