TIP 24 / 36

Load Count On-the-fly.

In addition to Eloquent's withCount() method to count related records, you can also load the count on-the-fly, with loadCount():

code
// if your Book hasMany Reviews...
$book = Book::first();

$book->loadCount('reviews');
// The model now exposes the reviews_count attribute.

// Or even with extra condition
$book->loadCount(['reviews' => function ($query) {
    $query->where('rating', 5);
}]);