Reputation: 1349
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
]
]
]
Upvotes: 0
Views: 1162
Reputation: 15319
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
Upvotes: 2