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

king_eke's avatar

PHP Variable Mutation

Hello Guys,

So i'm trying to build a report system that takes in optional parameters from & to which are dates.

so this is the function,


       	 $loans = new Loan();

        if ($request->from && $request->to) {
            $loans = $loans->whereBetween('created_at', [$request->from, $request->to]);
        }

so i didn't call get or all on any of them, cause i want to use the variable and do different queries on them.

so the problem is if I dump the count of loans when the from and to parameters are passed, lets assume of a random date


dd($loans->count());

I get around 194 applications

but then when i create a new variable and use the loans variable to query other things,


$test = $loans->whereNotNull('user_id')->whereNull('underwriter_level_1_user_id')->count();

and i dump the count of the loans variable that was originally 194 right after that.

the count reduces to 134. cause of this test variable.

Does anyone know why this is happening and how i can get around this? I want to leave the loan variable open to any query i want, and i don't want to call the model every time, cause i'll have to pass the wherebetween for each of them, and this is an optional field

0 likes
8 replies
martinbean's avatar

@king_eke If you want to add conditions to your query based on data in the query string, then you can do so with the when() method.

It takes a condition and a callback to invoke if that condition is truth-y:

$loans = Loan::query()
    ->when($request->has('from') && $request->has('to'), function ($query) use ($request) {
        $query->whereBetween('created_at', [$request->from, $request->to]);
    })
    ->whereNotNull('user_id')
    ->whereNull('underwriter_level_1_user_id')
    ->count();
king_eke's avatar

Hey @martinbean thanks for you answer. That can work. But is there any reason why the second variable query is overwriting my original query for the first variable ?

mkshingrakhiya's avatar

It is maybe because you've provided additional conditions in second statement. Try dd($loans->count()); after the second statement and it might still return the original count.

king_eke's avatar

@mkshingrakhiya yeah that's the reason. If I comment the test variable and do a count I'll get 194 for the loans variable. If i uncomment the second variable with the extra queries. And dump the count for the loans variable. It reduces the count to 132. So I want the loans variable to never be altered cause it's going to be queried like the test variable multiple times.

king_eke's avatar

@mkshingrakhiya hmm, could you do a test on your end?.. say whip up a laravel product and just do a factory of users with different created_at dates?

king_eke's avatar

@joefusco yeah that did the trick, thanks

using it like this works


$test = (clone $loans)->whereNotNull('user_id')->whereNull('underwriter_level_1_user_id')->count();
1 like

Please or to participate in this conversation.