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

eddy1992's avatar

Eloquent query

Hi I have a filter named prices when the user clicks on say Less than 20 check box all the products should filter through a table named product_prices_inventory_tax. So when the user checks the checkbox related to price range It sends an array of the price range.

say I clicked on Less than 20 then I get

Array
(
    [price_array] => Array
        (
            [0] => 0,20
        )

)

when user check a price range 21 - 50 then it would return an array

Array
(
    [price_array] => Array
        (
            [0] => 0,20
            [1] => 21,50
        )

)

So I want to query my PricesInventoryTax model where I would dynamically able to query. The query I made is this

 $productsPrice = ProductPricesInventoryTax::where('sale_price', '>=', $min_price)
                                                ->where('sale_price', '<=', $max_price)
                                                ->get(); 

The challenge is this would work for one price range say 0, 20. I can find the smallest and the largest value from this but what would i do when I get another array f price range ? How will I add another query say I want to find products ranging from 0,20 as well as 30,40 ?

0 likes
5 replies
jlrdw's avatar

Have you thought of putting some queries in an if else condition.

onyx26's avatar

I don't see why you'd allow more than one 'price range' to be selected. Surely it would be silly to have a low and high price range selected at the same time? I'm imaging the sort of thing you get on eCommerce sites, so perhaps I'm not seeing your use case in the same way as you.

I would say for price ranges use a radio button to allow only one to be selected, and for more flexibility just use a 'low value' and 'high value' field: either an input box or drop-downs.

I apprecaite this doesn't solve the technical issue - am thinking of it more as a design issue.

wheeler's avatar

@onyx26 I think it is pretty commonplace to be able select more than once price range at once, if you are prepared to spend between say $20 and $100 on something (maybe you don't quite know what you will get for your money), then that will likely span more than one of the developer defined price ranges.

Another good option is to allow the user to enter a range manually.

onyx26's avatar

Yeah, I don't think I made that clear. When I said a low and a high price band I meant something like 0-20 and 81-100 at the same time. You want a contiguous range, and check boxes aren't really designed for that. The range solution (low price to high price) would be much better. Unless I'm missing the design requirements here, I think such a range would avoid the problem the OP is having.

Please or to participate in this conversation.