Learn how to load Eloquent relationship counts
timacdonald wrote a nice tutorial on how to load Eloquent relationship counts.
It is often useful to show the number of related models a given instance has, but not actually need any specific information about the related models, just how many exist. In this scenario you do not want to load all of the related models into memory to count them, we want our database to do the heavy lifting for us. Laravel offers a number of ways to retrieve relationship counts. 2 have been around for a while, but there is a new kid on the block:
the query builder
the relationship
an eloquent collection
Link:
This post is submitted by our members. Submit a new post.
Tags: Tutorials Laravel 5.7 Laravel 5.6 Laravel 5.5 Laravel 5.4 Laravel 5.3 Laravel 5 Intermediate