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

umer228's avatar

Laravel price range search from multiple columns

I have to search price range(min_price & max_price) from two columns(regular_price & sale_price) but unable to get values from both columns.

0 likes
9 replies
Sinnbeck's avatar

Can you show your current code that doesn't work?

umer228's avatar

$products = $products->whereBetween('ecommerce_sku.regular_price', [$minPrice, $maxPrice]);

i tried this one by adding another column in it $products = $products->whereBetween(['ecommerce_sku.regular_price','ecommerce_sku.sale_price'], [$minPrice, $maxPrice]); but didn't work for me

umer228's avatar

Then someone suggested me this one ,

$string = '(ecommerce_sku.sale_price between "'.$minPrice.'" and "'.$maxPrice.'") AND (ecommerce_sku.regular_price between "'.$minPrice.'" and "'.$maxPrice.'")'; $products = $products->whereRaw($string);

this also not work for me

PovilasKorop's avatar

Should work:

$products->whereBetween('ecommerce_sku.regular_price', [$minPrice, $maxPrice])
    ->orWhereBetween('ecommerce_sku.sale_price', [$minPrice, $maxPrice])
    ->get();

But I'm not sure what $products is in your query - is it query object, or do you have a separate EcommerceSKU Eloquent model? So it may be more complicated.

1 like
umer228's avatar

this query is not working for there. $product is object that join multiple tables to complete a product.

hondnl's avatar

Please give us the whole query ( including $product) or we cannot see what is going wrong here.

umer228's avatar

$products->whereBetween('ecommerce_sku.regular_price', [$minPrice, $maxPrice]) ->orWhereBetween('ecommerce_sku.sale_price', [$minPrice, $maxPrice]) ->get();

that is the complete query. where $products have joins with multiple tables to get complete description about the Product.

hondnl's avatar

That is what I am asking , give us the query of products. We want to see what is happening there.

umer228's avatar

Product::active() ->join('ecommerce_sku', 'ecommerce_sku.product_id', '=', 'ecommerce_products.id') ->where('ecommerce_sku.status', 'active') ->groupBy('ecommerce_sku.product_id', 'ecommerce_products.id');

Please or to participate in this conversation.