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

Respect's avatar

Hello How To Apply This Query In Laravel

Hello How To Apply This Query In Laravel i need select all products price_before and price_after not = 0

i tested this
-------------------
$products = Product::where([
                 ['price_before', '!=', 0],
                 ['price_after', '!=', 0]
                 ])->get();
return ProductResource::collection($products);

and also this

$products = Product::where('price_before','!=',0)
                            ->where('price_after','!=',0)
                        ->get();
return ProductResource::collection($products);
But Both doesn't work  if one of them = 0  price_before =0 or price_after = 0    Product not show in the query any help

0 likes
12 replies
Sinnbeck's avatar

Can you show an example of a line from your database you would expect to get with this query?

Something like?

price_before | price_after
0             | 0                         <-should not be found
22            | 0                         <- should be found
1 like
Respect's avatar

@RESIN -

I'm Appreciate your time 
I need show 
price_before | price_after
0               |  1
1               | 0
1               | 1
But I dont need to show only if
price_before | price_after
0 | 0
My query in the question show only if 
price_before | price_after
1 | 1
Sinnbeck's avatar

That should work. Can you try this and post the result

dd(Product::where('price_before','!=',0)
                            ->where('price_after','!=',0)
                        ->toSql());

1 like
Respect's avatar

@RESIN - Here

"select * from `products` where `price_before` != ? and `price_after` != ?"

i dont know why it isnt work

Sinnbeck's avatar

Can you open you database directly (in heidisql or dbeaver etc) and copy/paste that query in?

select * from `products` where `price_before` != 0 and `price_after` != 0

Does it return any rows?

1 like
Respect's avatar
@RESIN - Give Me only product 1 | 1
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Try this instead

$products = Product::where('price_before','!=',0)
                            ->orWhere('price_after','!=',0)
                        ->get();
return ProductResource::collection($products);
1 like
Respect's avatar

Thanks So Much sir For your time it's worked But Why ?

Sinnbeck's avatar

The problem is that is says price_before AND price_after should both not be 0. So when one is 0 it fails The new one makes the following query

select * from `products` where `price_before` != 0 or `price_after` != 0

Notice that is says OR instead of AND. This implies that just one of them must be true to return a row

1 like

Please or to participate in this conversation.