Search code examples
phplaravellaravel-livewire

How to toggle a boolean value with livewire and checkboxes


I am trying to create this filter thing with check boxes.

I am trying to toggle the state of the check boxes to be true or false

<x-input.checkbox wire:click="$toggle(free_trial)"/>
    public $free_trial = false;
    public $free_forever = false;
    public $pay_per_user = false;
    public $progressive = false;

When I do this approach, I am not sure how to make the inverse work, so if it is unchecked It would be false. Is there a way to add something to this, so if it's clicked again it would change it to false?

<x-input.checkbox wire:click="$set('free_trial','true')"/>

Any help much appreciated :)


Solution

  • <x-input.checkbox wire:click="$set('free_trial',{{ $free_trial ? 'false' : 'true' }})"/>
    

    I prefer dev this kind of login in a backend method.

    <x-input.checkbox wire:click="toggleProperties('free_trial')"/>
    
    public function toggleProperties($property)
    {
       if($property == 'free_trial')
          $this->free_trial = !$this->free_trial;
       elseif(...) //.....
    }