I tried many ways of populating old checked values in edit page of livewire.
Method 1 :
public $role_id, $name, $new_permissions = [];
public function mount($id)
{
/
$old_permissions = \App\Models\UserRolePermission::where('user_role_id', $this->role_id)->pluck('permission_id')->toArray();
// here new_permissions is my wire:model name in view for the checkbox input
// so I tried to initialize the new_permissions array by old_permissions array
$this->new_permissions = $old_permissions;
$this->role_id = $id;
}
Method 2 :
Manually fetched the permissions in the view and checked in_array for the old_permissions to be checked:
@php
$old_permissions = \App\Models\UserRolePermission::where('user_role_id', $this->role_id)->pluck('permission_id')->toArray();
@endphp
<div class="col-md-12">
<div class="form-group">
<label class="display-block text-semibold">Select Permissions</label>
@foreach($permissions as $item)
<label class="checkbox-inline">
<input type="checkbox" wire:model="new_permissions" class="" value="{{ $item->id }}" {{ in_array($item->id, $old_permissions) ? "checked" : "" }}>
{{ $item->name }} </label>
@endforeach
</div>
</div>
In the 2nd method, my code in view source was correct, the old ones had checked in input field and the new ones didnt, but in the view the checkboxes didn't appear to be checked.