0

I have below two arrays

$vendor_permissions = [1,2,3,4]
$assigned_vendor_permissions = [1,3]

I have a table with checkboxes for all permissions in one row.

Now, I want to check only those checkbox which has $assigned_vendor_permissions, means, in this case, there are 4 checkboxes as per $vendor_permissions and 2 must be checked as permissions $assigned_vendor_permissions [1,3] assigned to the user.

I tried with the below loop, but I could not get success, as it repeats checkboxes more than 4 times,

@foreach ( $vendor_permissions as $vendor_permission )             
    @if ($assigned_vendor_permissions->isNotEmpty())
        @foreach ( $assigned_vendor_permissions as $rolem )                                        
            <td style="width: 15%"><label class="checkbox"><input type="checkbox" name="permissions[]" id="{{ $vendor_permission->name }}" 
            {{ $vendor_permission->id == $rolem->id ? 'checked="checked"' : '' }} value="{{ $vendor_permission->id }}""><span></span></label></td> 
        @endforeach   
    @else
        <td style="width: 15%"><label class="checkbox"><input type="checkbox" name="permissions[]" id="{{ $vendor_permission->name }}" 
            value="{{ $vendor_permission->id }}""><span></span></label></td> 
    @endif                          
@endforeach

How can I get these checkboxes checked and unchecked based on the array?

Output of dd($assigned_vendor_permissions);

array:2 [▼
  0 => array:6 [▶
    "id" => 1
    "name" => "vendor_create"
    "guard_name" => "web"
    "created_at" => null
    "updated_at" => null
    "pivot" => array:2 [▶
      "role_id" => 2
      "permission_id" => 1
    ]
  ]
  1 => array:6 [▶
    "id" => 2
    "name" => "vendor_read"
    "guard_name" => "web"
    "created_at" => null
    "updated_at" => null
    "pivot" => array:2 [▶
      "role_id" => 2
      "permission_id" => 2
    ]
  ]
]

1 Answer 1

2

Instead of loop,Use in_array

@foreach ( $vendor_permissions as $vendor_permission )

    <td style="width: 15%">
        <label class="checkbox">
            <input type="checkbox" name="permissions[]" 
                   id="{{ $vendor_permission->name }}"
                   value="{{ $vendor_permission->id }}"  
                {{(is_array($assigned_vendor_permissions)&&in_array($vendor_permission->id,$assigned_vendor_permissions))?"checked":null}}
            >
            <span>
                
            </span>
        </label>
    </td>

@endforeach
Sign up to request clarification or add additional context in comments.

6 Comments

How to use this for laravel collection if there are collections instead of an array for assigned permissions?
just return toArray()
now it shows all 4 checkboxes but doesn't have a checkbox checked with 3 permissions. All are unchecked.
first check dd($assigned_vendor_permissions) and show what you have
@rjcode which id do you want to check .is it "id" => 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.