Closed jameshulse closed 6 years ago
A clarification that I missed when first commenting; the problem is described in a code comment.
$monthly_payments->count() now returns 10 as the 'where' clause has been reapplied to the same query instance
Sorry that was probably a bit unclear, to re-iterate: Queries can be mangled by subsequent calls to Payment::settled()
if they have not been executed because the query instance is shared.
I'm sorry but I can't understand what this issue is about, can you please explain in more details?
Note that he's not executing ->get() at the end of those lines. The issue is that the second call to ->where(...)
is modifying the query instance that the first line has created. I have not verified this myself.
Unable to replicate on 5.5, running PHP 7.1.10
>>> $new_sponsors = App\Sponsor::active()->where('created_at', '>', '2017-11-01');
=> Illuminate\Database\Eloquent\Builder {#894}
>>> $old_sponsors = App\Sponsor::active()->where('created_at', '>', '2017-01-01');
=> Illuminate\Database\Eloquent\Builder {#907}
>>> $old_sponsors->count() //should return 1
=> 1
>>> $new_sponsors->count() //should return 0
=> 0
>>>
Tried changing the order of operations around (set new after old, check counts only after setting both, etc.) and could not get the first query to return the second query's count.
@36864 thanks for attempting to replicate.
I have a feeling tinker is doing something special between lines that isn't happening when it is being run the normal laravel execution path (such as in a controller).
Could you please try your test in a simple controller action? Maybe return the counts as 2 fields on a json response.
Can't replicate either, the output is select * from
userswhere
status= ?
, a new instance is assigned to $two
$one = \App\User::active()->whereName('themsaid');
$two = \App\User::whereName('taylor');
dd(
$two->toSql()
);
@jameshulse Sure.
Here's my test controller code:
$new_sponsors = Sponsor::active()->where('created_at', '>', '2017-11-01');
$old_sponsors = Sponsor::active()->where('created_at', '>', '2017-01-01');
dump($new_sponsors);
dump($old_sponsors);
dump($new_sponsors->count());
dump($old_sponsors->count());
And what gets dumped:
Builder {#473 ▼
#query: Builder {#472 ▶}
#model: Sponsor {#474 ▶}
#eagerLoad: []
#localMacros: []
#onDelete: null
#passthru: array:11 [▶]
#scopes: []
#removedScopes: []
}
Builder {#476 ▼
#query: Builder {#477 ▶}
#model: Sponsor {#526 ▶}
#eagerLoad: []
#localMacros: []
#onDelete: null
#passthru: array:11 [▶]
#scopes: []
#removedScopes: []
}
0
1
Different instances all around, got expected results.
Might be a 5.4 issue, can you try upgrading to 5.5 and see if the issue persists?
Closing this for now but please feel free to continue discussions. I suggest that you test in a fresh 5.5 app since 5.4 is not supported anymore.
Description:
The query passed to a local scope method on an eloquent model is re-used between static calls.
Steps To Reproduce:
In our code it looks something like this