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_ids:

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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -