Home > OS >  duplicate data in blade when send data
duplicate data in blade when send data

Time:05-31

i´m trying to show all my roles in my edit blade and with checkbox, that this checks it´s checked if this role have this permission.

my problem it´s when send data permissions and permissions to this role, all permission name it´s duplicate.

enter image description here

for set roles and permission i´m using laravel-permission/spatie

in my controller i have this:

public function edit($id)
    {
        $role = Role::find($id);
        $permissions = Permission::all();
        $permissionAssigned = Role::find($id)->givePermissionTo();

        return view('opciones.roles.edit', ['role' => $role, 'permissions' => $permissions, 
                                            'permissionAssigned' => $permissionAssigned["permissions"] ]);
    }

and in my blade i have this:

<h3>Permissions</h3>
                            <div > 
                              @foreach($permissions as $permission)
                                @foreach ($permissionAssigned as $asigned)
                                  @if($permission->name == $asigned->name)
                                    <div >
                                      <input  checked type="checkbox" name="permission[]" value="{{ $permission->name }}" id="flexCheckDefault">
                                      <label  for="flexCheckDefault">{{ $permission->name }}</label>
                                    </div>                                                                      
                                  @endif
                                @endforeach
                                <div >
                                  <input  type="checkbox" name="permission[]" value="{{ $permission->name }}" id="flexCheckDefault">
                                  <label  for="flexCheckDefault">{{ $permission->name }}</label>
                                </div>
                              @endforeach                                                             
                            </div>

i don´t know that i´m doing wrong. I know that i have a logic problem, but i can´t solve it.

Thanks for readme and sorry for my bad english

CodePudding user response:

You just need to pass the Role with associated Permissions and all Perrmissions to compare against, from the controller.

public function edit($id)
{
    $role = Role::with('permissions')->find($id);
    $permissions = Permission::all();

    return view('opciones.roles.edit', ['role' => $role]);
}

In the view

<h3>Permissions</h3>
<div > 
    @foreach($permissions as $permission)
        <div >
            <input type="checkbox"
                   id="flexCheckDefault"
                   
                   name="permission[]" 
                   value="{{ $permission->name }}" 
                   @checked((old($permission->name, $role->permissions->contains('name', $permission->name)) />
            <label  for="flexCheckDefault">{{ $permission->name }}</label>
        </div>   
   @endforeach                                                             
</div>
  • Related