Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

georgeos's avatar

Eloquent to query many tables

Hello all, I want to query four tables using Eloquent: status, type, activity, project. One project has many activities and one status, and every activity has one type. Using relationships I can query three of them, activity, project and type.

return Activity::with('project','type')->paginate(25);

But I want to know the status of the project. How can I do that with Eloquent? Thanks in advance.

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

You can eager load nested relations - I assumed the Project has one Status???

return Activity::with('project.status','type')->paginate(25);
1 like
jlrdw's avatar

Yeah that might work good for a few records but wait til you have a nested foreach and inner foreach has thousands of results. Eloquent does not work well when humongous result sets. A while back I demonstrated pagination Within pagination to handle this type of predicament. Folks keep getting these cute little collections and demonstrate the above foreach scenarios but I never see them take into account thousands of results.

Please or to participate in this conversation.