No comment.
$users = User::with('roles')->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am using Spatie Permissions and Roles, I want to return the role name but it isn't appearing.
$data = DB::table('users')
->join('model_has_roles', 'users.id', '=', 'model_has_roles.model_id')
->join('roles', 'model_has_roles.role_id', '=', 'roles.id')
->select('users.*', 'model_has_roles.*', 'roles.*')
->get();
Illuminate\Support\Collection {#1393 ▼
#items: array:1 [▼
0 => {#1399 ▼
+"id": 1
+"name": "admin"
+"email": "[email protected]"
+"email_verified_at": null
+"password": "5452352352352344DDASSFAGDGDAGDASGDGASDGASGASFFASDFAGH34TDAG"
+"remember_token": null
+"created_at": "2020-11-01 21:42:02"
+"updated_at": "2020-11-01 21:42:02"
+"role_id": 1
+"model_type": "App\Models\User"
+"model_id": 1
+"guard_name": "web"
}
]
}
$user->roles is a collection so what do you think?
You need use foreach or
{{ $user->roles->pluck('name')->implode(' ') }}
Please or to participate in this conversation.