0
votes

I am trying to get the role_id BUT i do not know how to do it because it does not work:

Auth::user->roles()->role_id

object(Illuminate\Database\Eloquent\Collection)#843 (1)

{ ["items":protected]=> array(1) { [0]=> object(App\Role)#839 (23) { ["table":protected]=> string(5) "roles" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(3) { ["id"]=> string(1) "6" ["role_title"]=> string(11) "FaceInicial" ["role_slug"]=> string(11) "faceinicial" } ["original":protected]=> array(5) { ["id"]=> string(1) "6" ["role_title"]=> string(11) "FaceInicial" ["role_slug"]=> string(11) "faceinicial" ["pivot_user_id"]=> string(2) "15" ["pivot_role_id"]=> string(1) "6" } ["relations":protected]=> array(1) { ["pivot"]=> object(Illuminate\Database\Eloquent\Relations\Pivot)#841 (26) { ["parent":protected]=> object(App\User)#817 (24) { ["table":protected]=> string(5) "users" ["fillable":protected]=> array(4) { [0]=> string(4) "name" [1]=> string(5) "email" [2]=> string(8) "password" [3]=> string(8) "username" } ["hidden":protected]=> array(2) { [0]=> string(8) "password" [1]=> string(14) "remember_token" } ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(9) { ["id"]=> string(2) "15" ["name"]=> string(13) "administrador" ["email"]=> string(13) "[email protected]" ["password"]=> string(60) "$2y$10$mC/LOcYzv3akuC1nFYawdOEzDINJq9pyVl8Ej4vY1XlcWfnODbkQy" ["remember_token"]=> string(60) "o2PV91bkFAAHYUjgy9Yd8ZIRtXgdrapByVLgeuWklcaKbSL0QAXtIvY4PQ6C" ["created_at"]=> string(19) "2018-10-15 11:27:42" ["updated_at"]=> string(19) "2019-02-11 13:13:49" ["username"]=> string(13) "administrador" ["deleted_at"]=> NULL } ["original":protected]=> array(9) { ["id"]=> string(2) "15" ["name"]=> string(13) "administrador" ["email"]=> string(13) "[email protected]" ["password"]=> string(60) "$2y$10$mC/LOcYzv3akuC1nFYawdOEzDINJq9pyVl8Ej4vY1XlcWfnODbkQy" ["remember_token"]=> string(60) "o2PV91bkFAAHYUjgy9Yd8ZIRtXgdrapByVLgeuWklcaKbSL0QAXtIvY4PQ6C" ["created_at"]=> string(19) "2018-10-15 11:27:42" ["updated_at"]=> string(19) "2019-02-11 13:13:49" ["username"]=> string(13) "administrador" ["deleted_at"]=> NULL } ["relations":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["forceDeleting":protected]=> bool(false) } ["foreignKey":protected]=> string(7) "user_id" ["otherKey":protected]=> string(7) "role_id" ["guarded":protected]=> array(0) { } ["connection":protected]=> NULL ["table":protected]=> string(9) "role_user" ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(false) ["attributes":protected]=> array(2) { ["user_id"]=> string(2) "15" ["role_id"]=> string(1) "6" } ["original":protected]=> array(2) { ["user_id"]=> string(2) "15" ["role_id"]=> string(1) "6" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) } } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) } } }

3
It's pretty difficult to read here. What does Auth::user->roles or Auth::user->roles() return? It seems like ->roles() return a collection, which means you can't simply run $collection->id. If this works, then you can use map to achieve what you want: Auth::user->roles->each(function($item) { var_dump($item->role_id` });senty
Please try to format your code, its impossible to read this way, thanks.Haris Bouchlis

3 Answers

2
votes

You should use the below solution.

$user = \Auth::user();
$name=$user->name;
$roles = $user->getRoles();

OR

You can try this:

Auth::user()->roles[0]->id
1
votes

As far as I understand, this should return what you want

Auth::user()->roles()->pluck('id') (or role_id, it's pretty difficult to read in above chunk)

If it does, in your User model, you can write

public function getRoles() {
   return $this->roles()->pluck('id')
}

And then,

Auth::user()->getRoles();

0
votes

Your log message is impossible to read but I see what wrong with your code.

You can't get attribute from array, you can only get it from single collection, so you have to select which collection you want, in my example I select first() one (with laravel special method).

Auth::user->roles()->first()->role_id;

Or you can do it with normal php way

Auth::user->roles[0]['role_id'];

note: you have to use roles in second example, you can't use roles() because it will return Relations(BelongsToMany in your case) class not collection