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) } } }
Auth::user->roles
orAuth::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