在 laravel auth 中设置自定义用户详细信息
Set custom user details in laravel auth
我在 smf 论坛中使用 smf_members table 在数据库中。有这样的字段:
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;
}
但我只能在调用时访问该信息:
User::find($id);然后有来自 smf_members.
的数据
我找不到任何方法将活动会话和数据放入用户模型和字段,来自
global $user_info;
var_dump($user_info);
我从第一个 "code" 获取数据的地方。
谢谢,请帮忙:)
您可以使用 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
访问属性
我在 smf 论坛中使用 smf_members table 在数据库中。有这样的字段:
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;
}
但我只能在调用时访问该信息: User::find($id);然后有来自 smf_members.
的数据我找不到任何方法将活动会话和数据放入用户模型和字段,来自
global $user_info;
var_dump($user_info);
我从第一个 "code" 获取数据的地方。
谢谢,请帮忙:)
您可以使用 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