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

checkuz's avatar

How to build and render the view for this situation [Eloquent + Blade]

I have a roles table:

| id  | name        |
| --- | ----------- |
|  1  | super-admin |
|  2  | admin       |
|  3  | manager     |
|  4  | user        |

The table has a pivot table to store the access to the roles:

| access_main_id | access_child_id |
| -------------- | --------------- |
| 1              | 1               |
| 1              | 2               |
| 1              | 3               |
| 1              | 4               |
| 2              | 2               |
| 2              | 3               |
| 2              | 4               |
| 3              | 3               |
| 3              | 4               |
public function access_to(): BelongsToMany
{
 return $this->belongsToMany(Role::class, 'role_access', 'access_main_id', 'access_child_id');
}

If I log-in as an admin user, and try to edit the manager it should show:

Desired output

Currently I'm able to show all the users where admin has access to when I edit the manager. But I need to check if the manager has access to any of these role -> admin, manager, user. If yes, then those roles must be checked. as shown in the above screenshot.

0 likes
1 reply
tykus's avatar

You need the list of Roles:

$roles = Role::whereNot('name', 'super-admin')->pluck();

You need the authenticated user's abilities (or at least their IDs):

$abilities = auth()->user()->role->access_to()->pluck('id');

Then in the Blade template (within the form) - the name attribute should be modified depending on your needs:

@foreach($roles as $role)
    <input type="checkbox" 
        name="access_to[{{$role->name}}]" 
        {{ $abilities->contains($role->id) ? 'checked' : '' }}
    >
@endforeach

Please or to participate in this conversation.