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

Torch's avatar
Level 1

Need help with simple query builder code

Maybe it's late and I am missing something obvious, but I am very confused. Can someone explain to me why this code

        $products = Product::where('id',15);

        $result = $products->get();

        dd($result);

return 1 (correct) result

and this code

        $products = Product::where('id',15);

        $a = $products->where('id',1111);
        $result = $products->get();

        dd($result);

returns zero results? I am not changing $products variable but $a variable. I can not do multiple "selects" on one predefined query?

Thank you Jan

0 likes
7 replies
Cronix's avatar

you actually are changing the $products object, which is a query builder object.

You're essentially doing

$products = Product::where('id', 15)->where('id', 1111)->get();

Anytime you are working with $products, you are building upon the query until you use ->get() (or first(), findOrFail(), etc), to execute the query, even if you saved it to $a. $a is now a copy of the $products query builder, but it's still the $products query builder.

Torch's avatar
Level 1

Thank you for clarification. Any tips/ideas how to workaround this? - to create one (complex) query and then modify it in the end slightly to fetch different results?

Cronix's avatar
$product1 = Product::where('id',15)->get();
$product2 = Product::where('id', 1111)->get();

Now that will actually execute 2 separate queries.

Cronix's avatar

You can't modify it if you already added it to the query builder object. It's best to do that logic first, and then use whatever you determine to build the query.

$a = 1;

if (someCondition) {
    $a = 2;
}

$product = Product::where('id', $a)->first();

So don't add the ID that you are querying by, until you actually know what ID to use in the query.

Torch's avatar
Level 1

The thing is that I have a very complex query that selects my products. Then based on this complex query i need to do other things with the results (further reduce it based on other conditions, etc...)

So I need the original results from query and also the reduced one

Well, I need to change the concept of my code then... thank you

impbob36's avatar

This will hit the database twice:

Product::where('id',15)->get();
Product::where('id',1111)->get();

But you you try something like:

// Hit database once
$products = Product::whereIn('id', [15,1111])->get();

// $products is now a collection so you can filter, map ...
$product15 = $products->where('id', 15);
$product15 = $products->where('id', 1111]);

Note: You don't need to use get on the collection. There are some awesome functions you can use to sort, transform: https://laravel.com/docs/5.5/collections

andreasbakir's avatar

i guess you are looking for something like this:

Product::where([
    ['id', '=', 15],
    ['id', '=', 1111],
])->get();

Please or to participate in this conversation.