I was following the article to bring up front end UI for Spatie/permissions.
I was able to understand every part but I cant understand a specific portion,
Please navigate to the Roles Part, (please search for 'roles.edit', it will take directly to the section )
In the Edit view the author is using the code,
<h5><b>Assign Permissions</b></h5>
@foreach ($permissions as $permission)
{{Form::checkbox('permissions[]', $permission->id, $role->permissions ) }}
{{Form::label($permission->name, ucfirst($permission->name)) }}<br>
@endforeach
My understanding is that the form collective uses third parameter as checked field.
when I run the output passed to the third field
{{$role->permissions }}
I am getting results in this format
[{"id":1,"name":"Permission","guard_name":"web","created_at":"2019-08-04 05:53:14","updated_at":"2019-08-04 05:53:14","pivot":{"role_id":9,"permission_id":1}},
I am not sure how the laravel collective is parsing the result.
Authors Controller for edit
public function edit($id) {
$role = Role::findOrFail($id);
$permissions = Permission::all();
return view('roles.edit', compact('role', 'permissions'));
}
Here is how I overcame this, btw I am not using collective
My Edit controller
public function edit($id)
{
$role = Role::find($id);
$roles_permissions = Role::findByName($role->name)->permissions;
$permissions = Permission::all();
return view('admin.userrole.role.edit', compact('role', 'permissions','roles_permissions'));
}
My Blade file
<h4>Assign Permission</h4>
<div class='form-group'>
<p>
Existing Permissions:
@foreach ($roles_permissions as $item)
<span class="badge badge-success"> {{$item->name}}</span>
@endforeach
</p>
<p> {{$role->permissions}}</p>
<select name="permissions[]" id="permissions" multiple class="form-control">
@foreach ($roles_permissions as $item)
<option value="{{$item->id}}" selected hidden></option>
<span class="badge badge-success"> {{$item->name}}</span>
@endforeach
<option value="" selected>
</option>
@foreach ($permissions as $permission)
<option value="{{$permission->id}}" checked="{{$role->permissions}}">{!!ucfirst($permission->name)!!}
</option>
@endforeach
</select>
</div>
I am sure that this is not the right way of achieving this. Please do let me know how do I populate the permissions in the select field.
Note: If devoting the question, kindly do put a comment why. So that I could improve my presentation. I do appreciate your effort. Thank you