php - how to assign first loop to second loop and so on in laravel?

i have a table Permission which has permission name, type, entity and I like to display it in the table while creating Role enter image description here

I pass three variables to create a page from controllers from one Class Permission

    $types=Permission::select('type')->groupBy('type')->get();
    $entities=Permission::select('entity')->groupBy('entity')->get();
    $permission=Permission::select('id','name')->get();

now how to display the following if Type=Admin, only display the following Entity and permission. here is the model of permission

    class Permission extends Model
{
    use HasFactory,Uuids;
 protected $fillable = ['id','name','type','entity'];
      public function roles()
    {
        return $this->belongsToMany(Role::class, 'permission_role');
    }
     
}

Thanks

Answer

Solution:

You need relationships.

your models:

class Type extends Model
{
    public function entities()
    {
        return $this->hasMany(Entity::class);
    }
}
class Entity extends Model
{
    public function permissions()
    {
        return $this->hasMany(Permission::class);
    }
}

your blade view:

<table>
    @foreach($types as $type)
        <tr>
            <th colspan="6">{{ $type->type }}</th>
        </tr>
        @foreach($type->entities as $entity)
            <tr>
                <th>{{ $entity->entity }}</th>
                @foreach($entity->permissions as $permission)
                    <td>{{ $permission->name }}</td>
                @endforeach
            </tr>
        @endforeach
    @endforeach
</table>

Source