Laravel : How to retrieve a column of intermediate table as an array? -
how retrieve column of intermediate table array?
for example:
intermediate table of users
, roles
,named role_user
,it this:
id user_id role_id
i want current user's role_id
s:
public function test() { //$roles collection. $roles=auth::user()->roles; //i want `role_id`s this: //$roleids=[1,2,3]; }
how it?
use eloquent relationship function in user model
public function roles() { return $this->belongstomany('app\role', 'role_user', 'user_id', 'role_id'); } public function getuserrole() { return $this->roles()->first(); }
if user has 1 role can access
auth::user()->getuserrole();
if user has many roles can call
auth::user()->roles;
Comments
Post a Comment