Yes @chrisgrim there was a typo there, nice catch! Thanks! I fixed my code sample.
As @martinbean highlighted you have a N+1 problem, for the reasons he outlined above.
But for the setup you need, options for eager loading it properly require a bunch of work.
You want to have:
- Eager load using limits
- paginated results for each relation
Eager loading with ->limit(...) is not supported out of the box, to eager load with ->limit(...) I would consider this package:
https://github.com/staudenmeir/eloquent-eager-limit
When eager loading we loose the ease of using the ->paginate() method. If you want paginated results on eager loaded relations you'll need to:
- Take care of
->skip() and ->limit() calls when eager loading related models
- Instantiate each paginator manually after fetching the results
This is a lot of work. But if your app will have a lot of users, that can access it simultaneously I would consider that.
One option is to use something like Livewire, hydrate the first call (skipping the N+1 problem), and let it handle the pagination asyncronously with ajax.
But that also requires some manual work to get it right.
If your app is for private or internal use inside an organization, and won't have many users/events, maybe the N+1 problem won't slow it down a lot.
I can try to come up with a proof of concept, but not today as it would require a lot of work, maybe on the weekend.
In the meantime, I hope this insights give you some guidance.
And if someone else want to help you on that, that would be wonderful.
Maybe opening a fresh thread to handle this improvement is a better way to ask for more help.