-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.2] Lazy Eager Loading issue in LengthAwarePaginator collection #14468
Comments
Can you give a code example for it? |
To explain this scenario, I'm going to use posts and comments relationship. $posts = Post::paginate(10); In Eloquent builder paginate() method, $total = $query->getCountForPagination();
$results = $total ? $this->forPage($page, $perPage)->get($columns) : []; In this scenario, In LengthAwarePaginater, this empty array got converted into support collection. My code to load comments for these posts using load() method is like below: $posts->load('comments'); Now load() fails because the given collection is Support collection not an Eloquent collection. |
Have you done it like this?
What results does the above line gives you? |
@srmklive I know with() works to load relations. But in some cases, I have to load relations after obtaining the results |
My suggestion would be that you utilize with to load relations. You can simply if the related data is empty or not. This is a better approach in my opinion:
|
@srmklive By using your way, it defeats the purpose of Eager loading. |
@vigneshgurusamy without taking a closer look at the code, I tend to agree with you that it should return an Eloquent collection, not a Base collection. That said, you can always wrap the results in an Eloquent collection yourself. You can just do It's a bit of a manual work-around, but at least it will get the job done. |
@jlem Thanks for the support. Am I afraid, I can not do that since I need the results as LengthAwarePaginator object with Eloquent collection as a paginator collection. |
Well the actual change occured in this PR: #14188 Mine was to fix some issue with it but it looks like the original PR has caused another issue, will submit a fix in a minute. |
After upgrading to Laravel 5.2.41, I'm getting Method load does not exist exception while trying to call load() method in LengthAwarePaginator collection.
I found out Eloquent Builder.php paginate() method sets empty array if total count of the result set is zero. So LengthAwarePaginator converts the empty array into \Illuminate\Support\Collection which does not have load() method.
Kindly return Eloquent empty collection instead of an empty array, so eager loading won't fail.
The text was updated successfully, but these errors were encountered: