I want to create a list of users with their specific role (or roles if multiple roles given) in Laravel when I stuck in this issues. The list I am trying to accomplish is like this;
In my controller:
use DB;
use Auth;
use Storage;
use App\Role;
use App\HasRoles;
use App\User;
use App\Profile;
$users = User::with('roles')->where('name', 'admin')->get();
return view('dashboard.users.index', compact('users'));
but I am having this error
if I dd($users);
I can see that the roles are there
USER model
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable, HasRoles;
protected $fillable = [
'name', 'email', 'phone_number', 'avatar', 'password', 'verification_code'
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
'phone_verified_at' => 'datetime',
];
public function profile()
{
return $this->hasOne('App\Profile', 'user_id','id');
}
public function mailingAdd()
{
return $this->hasOne('App\MailingAddress', 'user_id','id');
}
user/index.php
@foreach($users->role as $item)
{{ $loop->iteration }}
{{ $item->name }}
{{ $item->email }}
{{ $item->role()->name }}
{{ $item->phone_number==null ? 'None' : $item->phone_number }}
@if($item->phone_verfied_at==null)
not yet verified
@else
verified
@endif
@endforeach
Changing @foreach($users->role as $item)
into @foreach($user->roles as $item)
gives me “Property [roles] does not exist on this collection instance”
USER TABLE MIGRATION
ROLES AND PERMISSION TABLE MIGRATION
Explanation
Since I am calling $user->role()
instead of $user->roles(),
To get them as an array to loop through you need to do
foreach($user->roles as $role){
// do something with role here
}
But doing this gives me “Property [roles] does not exist on this collection instance”
AS you can remember I said on the first part of this post is I want to create a list that display the list of roles per user like this;
The Solution I came out
@foreach($users as $user)
{{ $user->name }}
@foreach ($user->roles as $role)
{{ $role->name }}
@endforeach
@endforeach
That code display the user list with their specific user role or roles if the user given multiple roles.
Hope this helps.
While I’m currently committed to an eight-hour workday in my current employment, I have ample availability to dedicate an additional four to six hours daily to projects. With nearly a decade of experience in web design and development, I’ve honed efficient time management skills to balance both professional and personal endeavors seamlessly. Feel free to reach out, and I’d be delighted to assist you.
I am currently employed and working for only 6 hours per day so I still, have plenty of time to do an extra 6 to 8 hours per day. I have been doing web design and development for almost a decade now so I know how to properly manage my time without compromising both my work and personal projects. Contact me and I will be very happy to help you.
© 2025 Cessto Web Solutions. All rights reserved