jeudi 21 novembre 2019

Eloquent checkbox search query using multiple parents & or clauses

I have a stock list of around 5,000 items to filter using multiple checkboxes. Some of the checkboxes will be OR clauses and some won't. My search checkboxes are broken up into categories for example;

Type Car, Truck, SUV

Make BMW, Audi, RangeRover, Ford

Model Data based on previous search criteria database-driven, unknown how many checkboxes or their names

I've started my query something like this following;

// Search based on type.
if ($request->has('car')) {
    $StocklistItem->orWhere('car', 1);
}

if ($request->has('truck')) {
    $StocklistItem->orWhere('productType', 1);
}

This works fine but when I apply the 2nd group of filters:

// Search based on model. 
if ($request->has('BMW')) {
    $StocklistItem->orWhere('productMake', 'BMW');
}

Now the search doesn't work because both groups use orWhere. I was thinking of having hidden inputs within each group and if a selection was made within that group updating that hidden input. So I could start to group each category of filters and possibly have their "child" checkboxes stored in an array.

Would that be a suitable approach to build up a dynamic search query like this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire