Answer the question
In order to leave comments, you need to log in
How to group checkbox by their entities?
Hello.
There is a table with permissions
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('entity');
});
<div class="form-group">
<div class="col-lg-10">
@foreach($permissions as $permission)
<input {{ in_array($permission->id, $rolePermissions) ? "checked" : null }} name="permissions[]" value="{{ $permission->id}}" type="checkbox" />{{ $permission->name}}</label>
@endforeach
</div>
</div>
Answer the question
In order to leave comments, you need to log in
The easiest option is to loop through them
$permissions = Permission::all();
$permissionEntity = [];
foreach($permissions as $permission){
if(!isset($permissionEntity[$permission->entity]){
$permissionEntity[$permission->entity] = [];
}
$permissionEntity[$permission->entity][] = $permission;
}
@foreach($permissionEntity as $entity => $permission)
<h4>{{ $entity }}</h4>
<div class="form-group">
<div class="col-lg-10">
<input {{ in_array($permission->id, $rolePermissions) ? "checked" : null }} name="permissions[]" value="{{ $permission->id}}" type="checkbox" />{{ $permission->name}}
</div>
</div>
@endforeach
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question