Laravel 5.3 withCount() nested relation

Laravel 5.3 withCount() nested relation

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Navigating Nested Relations in Laravel 5.3: The withCount() Limitations

When working with deeply nested relationships, such as a Tutorial having many Chapters, each of which has many videos, you often need to know the count of the deepest level without triggering an eager load that bloats your memory. In Laravel 5.3, achieving this with the withCount() method is straightforward for one level but becomes tricky when traversing multiple levels.

Consider a model structure where Tutorial relates to Chapters via a hasMany relationship, and Chapters relate to videos in the same way. A common requirement is to retrieve tutorials along with the total number of videos attached to them, labeled as video_count. The natural instinct when dealing with eager loading is to chain withCount() calls:

Tutorial::withCount('chapters')
    ->withCount('chapters.videos')
    ->all();

However, this approach fails in Laravel 5.3. Attempting to use a nested path like 'chapters.videos' directly within withCount() results in the error: Call to undefined method Illuminate\Database\Query\Builder::chapters.videos(). This happens because withCount() expects a relationship name or a simple string representing a single relationship, not a dotted notation for nested eager loading. The query builder does not support calculating counts across multiple relationship hops using this specific syntax in version 5.3.

To overcome this limitation, you must decouple the eager loading of the relationship from the counting logic. The solution involves using with() to eagerly load the nested relationship first, and then applying withCount specifically to that loaded relationship's scope. Here is how it works:

Tutorial::with([
    'chapters' => function ($query) {
        $query->withCount('videos');
    }
])->get();

In this pattern, the Tutorial model eagerly loads its chapters. Inside the closure passed to the chapters relationship, we apply withCount('videos') to the query builder for the Chapters model. This ensures that every chapter is loaded with its associated video count attached to it. Consequently, when you access $tutorial->chapters[0]->videos_count, the data is already available without executing a separate SQL query for each chapter.

It is important to note what this approach actually returns. While the Tutorial model might have a video_count accessor if you define one, the nested structure means the count lives on the Chapter entity. If your goal is strictly to get a flat video_count on the Tutorial model representing the sum of all videos across all chapters, Laravel's relational query builder does not provide a built-in method to aggregate counts across multiple hasMany relationships in a single step within version 5.3.

If you require the total count of videos aggregated directly on the Tutorial model, you might need to calculate it manually or use a raw SQL subquery. However, if your application logic allows accessing the count per chapter, the with() closure approach is the most efficient native solution available in 5.3.

For projects requiring more advanced query aggregation across nested relations, consider upgrading to newer Laravel versions where relationship composition and eager loading capabilities have been significantly expanded. For now, understanding the boundaries of withCount() in 5.3 ensures you write queries that are both performant and compatible with your framework version.

Our network