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

Constantine_92's avatar

How i filter my product car by category_id with multiple selection (checkbox)

Good afternoon im new in laravel -livewire and i try to make sidebar filters for my project i cant make multiple selection by id . Any idea or any doc for that

0 likes
7 replies
alessandro.mirabelli's avatar

I imagine you already have the sidebar loading all the categories and relative ids.

Then on flagged checkbox goes to livewire action and add the ids to a Wherein().

Can I ask if this is the only filtering that you plan to do?

1 like
Constantine_92's avatar

thank you for response !! no the sidebar will have many filters like model, brand ,years I would like to use $ querystring ,so that I can use a listener to combine compnent.

can I somehow send you printscreen in this forum?

alessandro.mirabelli's avatar

@Constantine_92 so i think something generically like:

view:
foreach(some parameter){
livewire model checkbox (put values and ids...)
}

component:
"some query"->when($this->"some parameter", function($query,"some parameter" ){ query->whereIn(...)}

...i dont know the perfomance :)...but if is ugly and works it anyway a start

Constantine_92's avatar

public $SelectedCat = [];

public $queryString=[
    'SelectedCat',
];

public function SelectCategory()
{
    dd($this->SelectedCat);
}

public function render()
{

        $carcategories =CarCategory::all();
         return view('livewire.cars-index',[
         'cars'=>Car::with('user','carcategory')
         ->when($this->SelectedCat = array_filter($this->SelectedCat), function($query)
         {
            return $query->whereIn('carcategory_id', array_keys($this->SelectedCat)) -> get();
            
         }),

         'carcategories'=>$carcategories,
         ]);
}

}

this way it works but when the category is not selected(is empty ) it does not bring anything back. how i work with if statement to fix it . Any idea

1 like
Constantine_92's avatar

public function render() {

    if(empty($this->SelectedCat)) {
        $cars = Car::all();
    }
    else
    {
        $this->SelectedCat = array_filter($this->SelectedCat );
        $cars= Car::with('user','carcategory')->whereIn('carcategory_id', array_keys($this->SelectedCat)) -> get();
    }
        
    $carcategories =CarCategory::all();

    return view('livewire.cars-index',['cars'=>$cars,'carcategories'=>$carcategories]);
}

}

i did this but when i click and after unclick the category again display empty cars

Please or to participate in this conversation.