0

I'm having smf forum where using smf_members table in db. There are fields like this:

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"
}

Also i have laravel model "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;
}

But i can only acces that info when i call: User::find($id); then there is data from smf_members.

I can not find any way to put active session and data to User model and fields, from

 global $user_info;
 var_dump($user_info);

Where i'm getting data from first "code" there.

Thanks, please help :)

PooX
  • 71
  • 8
  • 1
    Why would you want to declare global user? If you want to access current logged in user just used `Auth::user()` or `auth()->user()` – bunchhaychea Dec 21 '19 at 17:18
  • I use global $user_info to get data from SMF SSI file, where i'm getting SMF session. I don't want two login/register form in laravel. Want to replace data from Auth::user() and smf data. Just put smf data to laravel auth. xD – PooX Dec 21 '19 at 17:22
  • You might want to look into this https://laravel.com/docs/6.x/views#sharing-data-with-all-views – bunchhaychea Dec 21 '19 at 17:27
  • But then i can't use laravel permission and other package. Using model is easiest and best way to do that, but i just want to set user as current smf user id. – PooX Dec 21 '19 at 17:30

1 Answers1

1

You can use eloquent accessors. Update your model to:

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
    }
}

Then you can access the attribute by User::find($id)->smf_info

bunchhaychea
  • 191
  • 2
  • 9