RoyGoode
1 month ago
203
5
Laravel

how to update livewire components data with scopeWithFilters?

Posted 1 month ago by RoyGoode

like this : its return the series but no filter! but URL = series?category=1 its work.

    public $category = [];
    public $order = '';

//// return data and update component

public function updated()
    {
        return Series::where('approved' , 1)->withFilters($this->category,$this->order)->paginate(9);
    }

    public function render()
    {
        return view('livewire.pages.series-page' , [
            'series' => $this->updated(),
        ]);
    }

component :

 <li>
      <input class="form-check-input" type="radio" name="order" id="latest" wire:model="order" value="latest" checked>
      <label class="form-check-label" for="latest">
                  latest
      </label>
</li>

 <li>
      <input class="form-check-input" type="radio" name="order" id="oldest" wire:model="order" value="oldest" checked>
      <label class="form-check-label" for="oldest">
                  oldest
      </label>
</li>

//// category

<ul>
          @foreach($categories as $category)
                  <li>
                       <input class="form-check-input" type="checkbox" name="category" wire:model="category" value="{{$category->id}}">
                         <div class="d-flex justify-content-between">
                               <label class="form-check-label">
                                      {{$category->name}}
                               </label>
                         </div>
                    </li>
           @endforeach
 </ul>
/// series model :

    public function scopeWithFilters($query)
    {
        $category = request('category');
        if( isset($category) && trim($category) != '') {
            $query->whereHas('categories' , function ($query) use ($category) {
                $query->whereId($category);
            });
        }

        if(!request('order') == 'latest') {
            if(request('order') == 'oldest') {
                $query->oldest();
            }
        } else {
            $query->latest();
        }

        return $query;
    }

Please sign in or create an account to participate in this conversation.