Support the ongoing development of →
posted 7 years ago
Last updated 6 months ago.

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')
    ->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 7 years ago.

@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.


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.


Sign in to participate in this thread!


Your banner here too?


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

Your logo here?

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

© 2022 - All rights reserved.