Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymouse703's avatar

how to show spatie roles in view?

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
0 likes
5 replies
SilenceBringer's avatar
Level 55

@anonymouse703

@foreach ($users as $user)
    @foreach ($user->roles as $role)
    	<td>{{ $role->name }}</td>
    @endforeach
@endforeach
anonymouse703's avatar

By the way sir how you can get the associated permissions of the specific role and map it in the checkbox?

I already try this but got some errors.

public $roleId, $name;
public $permission = [];

public function roleId($roleId){
        $this->roleId = $roleId;
        $role = Role::find($roleId);   
        // $this->name = $role->name; // this is in my role name in view
        // $this->permission = $role->map(fn ($permission) => $permission->id)->toArray(); // my check box permissions
    }
SilenceBringer's avatar

@anonymouse703

public $roleId, $name;
public $permission = [];

public function roleId($roleId){
        $this->roleId = $roleId;
        $role = Role::find($roleId);   
        $this->permission = $role->permissions()->pluck('id')->toArray();
    }

Please or to participate in this conversation.