Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

I don't understand your question. Do you keep all the books in your $books? You should paginate in ORM, not in ready collection.

If you want to eager load authors, you should use with() method, like this:

$books = Book::with('author')
    ->orderBy('title')
    ->skip(20)
    ->take(10)
    ->get(['title', 'excerpt']);

I learned that I don't need to operate on collections of models very often. Sometimes I do, but generally most of stuff you should do on the query itself.

Last updated 9 years ago.
0

@rickshawhobo - Just as a point of support, I'm in the same boat as you here. I want to be able to get my result set, THEN add the eager loading so I'm not loading the relationships on ALL of the records, then paginating them.

0

The solution is this

$books = Book::with('author')->paginate(10);

The problem is that you just can't lazy eager load with pagination. I'm not sure why they made that decision.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.