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

Respect's avatar

Query Keeps return data if Match or doesn't match id

Query Keeps return data if Match or doesn't match id

// Api.php
Route::get('/products/category/{category}','ProductController@productsCategory');
 public function ProductsCategory($id)
    {
        $products = Product::where('category_id', $id)
                    ->where('price_before','!=',0)
                    ->orWhere('price_after','!=',0)
                    ->paginate(10);

        return ProductCollect::collection($products);

    }
If i write in broswer 
--------------------------
http://127.0.0.1:8000/api/products/category/5

return products of category_id 5  // works 

But If i wrote 4 or 3 or 2 or what ever in broswer link  Keep Return the same data of category_id 5

What i Did

php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
0 likes
6 replies
chrisostomKaweza's avatar
Level 1

@Respect hope this helps Product::where('category_id',$id)->where(function ($query) { $query->where('price_before','!=',0) ->orWhere('price_after','!=',0); })->paginate(10);

1 like
Respect's avatar

@CHRISOSTOM - You Are Awesome Thanks So Much Sir but Why is wrong use multiable where with out clousure

chrisostomKaweza's avatar

Its because you have to first select the products by id thus the first where then you group the other where according to condition. In normal query it could be written as select * from products where category_id = $id and (price_before !=0 or price_after ! =0) you see that bracket grouping......

Happy to help also

1 like

Please or to participate in this conversation.