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

Victor Alfonso's avatar

How to convert this query in eloquent

I would like to convert this query in eloquent

$lims_product_data = Product::where('code', $product_code[0])->first();

and add a extra condition, that will be a not in clause to get all the products in the procut table but execept the products with category_id = 5.

i do that in mysql with the query SELECT * FROM products where is_active=1 and category_id NOT in (5) The first where in the original eloquent query is mandatory so, i only need use "not in" clause

0 likes
4 replies
piljac1's avatar
piljac1
Best Answer
Level 28
Product::where('is_active', 1)->whereNotIn('category_id', [5])->first();

Or if you want to keep your initial query as well as well as the SQL query you posted

Product::where('code', $product_code[0])
    ->where('is_active', 1)
    ->whereNotIn('category_id', [5])
    ->first();

Of course, the [5] will probably be dynamic, so update the above appropriately.

If you only have one category, you can simply use ->where('category_id', '!=', 5)

1 like
Victor Alfonso's avatar

it will be correct like this?

$lims_product_data = Product::where('code', $product_code[0])->whereNotIn('category_id', [5]);->first();

piljac1's avatar

Assuming $product_code[0] is valid and that you have a global scope that sets is_active to 1, yes.

1 like

Please or to participate in this conversation.