Search code examples
phplaravellaravel-livewire

Incomprehensible behavior of checkboxes in Livewire


The checkboxes of the selected checkboxes are displayed correctly for a fraction of a second when the page is loaded, but they disappear at the end of rendering. If you delete wire:model, the checkboxes are always displayed correctly as expected. Help me understand what is the reason for this behavior?

My Livewire Controller

public $allProperties;
public $filterProperties;
public $currentGroupFilter;

public function updatedFilterProperties(){

    if ($this->filterProperties){

        if (isset($this->currentGroupFilter)){
            session([
                    'filterScope-'.$this->currentGroupId() => $this->currentGroupFilter + array_filter($this->filterProperties),
                    ]);
        } else {
            session([
                    'filterScope-'.$this->currentGroupId() => array_filter($this->filterProperties),
                ]);
        }
    }
}

public function mount($parentGroup)
{
    
    $this->allProperties = $this->getProperties($this->productsId);

    if (session('filterScope-'.$this->currentGroupId())){
        $this->currentGroupFilter = session('filterScope-'.$this->currentGroupId());
//      $this->currentGroupFilter = array_map(fn($g) => ($g == true),$this->currentGroupFilter,);
        $this->updatedFilterProperties();
    }
}

And my blade

@foreach($allProperties  as $property)
    <div x-data="{ open: true }" class="border border-silver border-md p-2">
        <button @click="open = !open">{{$property->name}}</button>
        <ul x-show="open">
            @foreach($property->values()->wherePivotIn('product_id', $productsId)->get()->unique() as $propertyValue)
                <li>
                        <input wire:model="filterProperties.{{$propertyValue->id}}"
                               @if($currentGroupFilter)
                                   @checked(array_key_exists($propertyValue->id,$currentGroupFilter))
                               @endif
                               id="{{$propertyValue->id}}"
                               value="{{$propertyValue->id}}"
                               name="{{$propertyValue->id}}"
                               type="checkbox"
                               class="h-4 w-4 border-secondary-300 rounded text-indigo-600 focus:ring-indigo-500">
                        <label for="{{$propertyValue->id}}"
                               class="ml-3 text-sm text-gray-600"> {{$propertyValue->value}}
                        </label>
                </li>
            @endforeach
        </ul>
    </div>
@endforeach

In updatedFilterProperties() i intercept filterProperties and conditionally create or add elements to the session array.

In mount() you may notice in the commented line an attempt to convert currentGroupFilter to the form id=>true, but that didn't help either.

In blade, I successfully get currentgroupFilter and, as I already wrote, in principle, for a fraction of a second during rendering, the selected checkboxes are displayed correctly, but at the end of rendering they disappear.

I don't know what I'm doing wrong....


Solution

  • As it turned out, the @checked check does not work in livewire. The solution was to change the input in the blade view

    <input wire:model="filterProperties.{{$propertyValue->id}}"
                                                               id="{{$propertyValue->id}}"
                                                               type="checkbox"
                                                               class="h-4 w-4 border-secondary-300 rounded text-indigo-600 focus:ring-indigo-500">
                                                        <label for="{{$propertyValue->id}}"
                                                               class="ml-3 text-sm text-gray-600"> {{$propertyValue->value}}
                                                        </label>
    

    mount():

        public function mount($parentGroup)
    {
        $this->currentGroupId = $parentGroup->id;
        session([
            'currentGroupId' => $this->currentGroupId,
        ]);
        $this->products_tpl = $this->getProducts($this->currentGroupId())->cursorPaginate(30);
        $this->products = collect($this->products_tpl->items());
        $this->childGroups = $parentGroup->childGroups;
        $this->allProperties = $this->getProperties($this->productsId);
        if (session('filterScope-'.$this->currentGroupId())){
            $this->filterProperties = session('filterScope-'.$this->currentGroupId());
            $this->updatedFilterProperties();
        }
    }
    

    and updated():

    public function updatedFilterProperties(){
    
        if ($this->filterProperties){
                session([
                    'filterScope-'.$this->currentGroupId() => array_filter($this->filterProperties),
                ]);
        }
    
        if (Arr::first(session('filterScope-'.$this->currentGroupId())) != null){
            $this->products_tpl = $this->getProducts($this->currentGroupId())->whereHas('values', function ($q) {
                $q->whereIn('property_value_id', array_keys(session('filterScope-'.$this->currentGroupId())));
            })->cursorPaginate(30);
        }
    
        if (Arr::first(session('filterScope-'.$this->currentGroupId())) == null){
            $this->products_tpl = $this->getProducts($this->currentGroupId())->cursorPaginate(30);
        }
    
        $this->products = collect($this->products_tpl->items());
    
    }
    

    Now the selected checkboxes are loaded as expected from the array that is stored in the session, and also based on this array, we filter the result in advance.