Level 55
@foreach ($users as $user)
@foreach ($user->roles as $role)
<td>{{ $role->name }}</td>
@endforeach
@endforeach
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How to loop this role in view?
#relations: array:1 [▼
"roles" => Illuminate\Database\Eloquent\Collection {#1442 ▼
#items: array:5 [▼
0 => Spatie\Permission\Models\Role {#1448 ▼
#guarded: array:1 [▶]
#connection: "mysql"
#table: "roles"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [▶]
#original: array:8 [▼
"id" => 1
"name" => "Superadmin"
"guard_name" => "web"
"created_at" => null
"updated_at" => null
"pivot_model_id" => 4
"pivot_role_id" => 1
"pivot_model_type" => "App\Models\User"
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
-permissionClass: null
}
1 => Spatie\Permission\Models\Role {#1449 ▶}
2 => Spatie\Permission\Models\Role {#1450 ▶}
3 => Spatie\Permission\Models\Role {#1451 ▶}
4 => Spatie\Permission\Models\Role {#1452 ▶}
]
}
and this the view
@forelse ($users as $user)
<td>I want to loop the roles here</td>
@endforeach
@foreach ($users as $user)
@foreach ($user->roles as $role)
<td>{{ $role->name }}</td>
@endforeach
@endforeach
Please or to participate in this conversation.