Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amitshrestha221's avatar

Laravel livewire checkbox options not being checked.

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.

0 likes
2 replies
Snapey's avatar

in the component, the checkbox will be a key corresponding to the wire:model name and then value of 'on'

You should not try and set the selected state within the view.

In the loop, every checkbox needs a different name. You cannot have all checkboxes have the same wire:model name.

amitshrestha221's avatar

Sir, my checkbox values are dynamic. In this case, If i set different names to the wire:model, do I need to define property of each different wire:model name in my component?

Please or to participate in this conversation.