If you are looking for an example of Eloquent Global Scope feature, then you’re in the right place!
Check the ready-to-use demo at …
Previously I showed you how to get single related model per each parent, while eager loading the
hasMany relation. That was pretty easy and we just made the most of the
However, most likely the time will come, when you need several related models per parent.
It won’t be as easy, but it’s far from impossible, so let’s do it!
Have you ever needed to show only single related model from the
hasMany relationship on a set of parents?
Being it latest, highest or just random, it’s not very clever to load whole collection using eager loading, just like running query per every parent.
Of course you can do that better, and now let me show you how. Continue reading