Reputation: 55
I have a "big" query with 10 joins, and finally a single where condition.
I also have about 6 arrays, each being filled with a select statement from said query.
$first_array = $query->select()->first();
In two of the arrays, I get the data using get()
instead of first()
. The issue is, the get()
method is only returning one row, when I know for a fact that there are two that match the condition. I used the toSql()
method to see the resulting query, only to find there is a limit 1
being appended to the query that I did not place. Is the limit being appended because I'm using first()
in some of the queries, which is limiting my results to one row? I've tried executing the query in my database manager and after removing the limit 1
from the query it does return the two rows I'm expecting.
Upvotes: 0
Views: 476
Reputation: 55
The issue was, if you did this like I did:
$array1 = $query->select()->first();
$array2 = $query->select()->first();
$array3 = $query->select()->first();
$array4 = $query->select()->first();
$array5 = $query->select()->get();
The first()
method calls set the limit 1
, and using get()
afterwards does not remove the limit.
If you use get()
first then first()
like so:
$array5 = $query->select()->get();
$array1 = $query->select()->first();
$array2 = $query->select()->first();
$array3 = $query->select()->first();
$array4 = $query->select()->first();
then the issue does not occur.
Upvotes: 2
Reputation: 523
Replace first()
with get()
is the answer. first()
is for retrieving a single model: https://laravel.com/docs/8.x/eloquent#retrieving-single-models where get()
returns you the collection of results: https://laravel.com/docs/8.x/eloquent#collections
Upvotes: 1