0

我有 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);

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

谢谢,请帮忙:)

4

1 回答 1

1

您可以使用 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

于 2019-12-21T17:41:58.980 回答