Show checkbox checked from database in laravel

11,683

Solution 1

Your code will not work because you are trying to compare array with a string, which is impossible. you can use php in_array function to check whether your permission exist for the current permission or not

I think you are trying to check all the permission which already exist for the specific roles. correct me if i am wrong.

Try this

<input type="checkbox" name="permissions[]" value="{{$permission->id}}" 
@if($role->permissions) @if(in_array($permission->id, $role->permissions->pluck('id')) checked @endif @endif>

Hope this will help :)

Solution 2

Easiest way to do is to check in collection using contain

@if($role->permissions->contains($permission->id)) checked=checked @endif

<input type="checkbox" name="permissions[]" value="{{$permission->id}}" 
{{ @if($role->permissions->contains($permission->id)) checked=checked @endif }}
>
Share:
11,683
psudo
Author by

psudo

Updated on June 16, 2022

Comments

  • psudo
    psudo almost 2 years

    I'm using Laratrust package for ACL in my application. I'm trying to edit roles (assign permissions to role) using checkbox. And want get already assigned permission checkbox state checked from database.

    Code in RoleController.php

        public function edit($id)
    {
      $role = Role::where('id', $id)->with('permissions')->first();
      $permissions = Permission::all();
      return view('admin.manage.roles.edit')->withRole($role)->withPermissions($permissions);
    }
    

    Below is the code what I have tried:

    @foreach($permissions as $permission)
     <div class="checkbox checkbox-styled">
       <label>
        <input type="checkbox" name="permissions[]" value="{{$permission->id}}" 
        {{ $role->permissions->pluck('id') == $permission->id ? 'checked' : '' }}
        >
        <span>{{$permission->display_name}}  <em>({{$permission->description}})</em></span>
    </label>
    </div>
    @endforeach
    

    The code is throwing error Object of class Illuminate\Support\Collection could not be converted to int

    I had tried:

    {{ $role->permissions->id == $permission->id ? 'checked' : '' }}

    This throws error:

    Property [id] does not exist on this collection instance

    When I do {{dd($role->permissions)}}: The following output was given:

    enter image description here

    enter image description here

    I would be very thankful if anyone could point-out mistake I'm doing here.

    • Vision Coderz
      Vision Coderz over 6 years
      can you echo "<pre>"; print_r($role->permissions); and show
    • Classified
      Classified over 6 years
      You're trying to check if a collection is equal to an int. $role->permissions is a collection. Try editing your post, while including your controller.
    • psudo
      psudo over 6 years
      @Classified I've updated the question with code from my controller.
  • psudo
    psudo over 6 years
    Vikash with your code snippet. I'm getting error in_array() expects parameter 2 to be array, object given
  • psudo
    psudo over 6 years
    Solved the problem by attaching toArray() method at the end of pluck method. Thanks for correcting me.