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

Chris1989's avatar

Livewire Checkbox On foreach Loop

I have a simple checkbox that i want has tick on 0 and untick on 1 value on every row, but the strange is doesnt get the id

   <input type='checkbox'  wire:model="hasnotify.{{ $service->id }}" class="py-2 form-checkbox  text-green-600">
    public function updatedHasnotify()
    { 
        // dd("$this->serviceID");
        Service::find($this->serviceID)->update(['hasnotify' => $this->hasnotify]);
    }

when i dd the serviceID i get null When i dd $this->hasnotify i get > array:1 [▼ 12 => true ] that is correct, but how i can separate that value to find the '12' id and take the true value?

0 likes
2 replies
Chris1989's avatar
Chris1989
OP
Best Answer
Level 1

I dont know if its 'dump' approach but it worked

 @if ($service->hasnotify == 1)
   <input type='checkbox' checked="true" value="{{$service->id}}" wire:click="hasnotify ({{ $service->id }},0)">
 @else
   <input type='checkbox' value="{{$service->id}}"  wire:click="hasnotify ({{ $service->id }},1)">
 @endif
                                      
   public function hasnotify($serviceID, $action)
    {
        Service::find($serviceID)->update(['hasnotify' => $action]);
    }
imfaisii's avatar

@chris1989 You can do it as :

// blade

<input wire:model="check.{{$service->id}}" type="checkbox" @if({{$service->hasNotify}}) checked @endif >

// backend file

public function updatedCheck($value, $id){ Service::find($id)->update(['hasNotify' => $value]); }

1 like

Please or to participate in this conversation.