Retrieving relationships of relationships using Eloquent in Laravel

Retrieving relationships of relationships using Eloquent in Laravel

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

Stefan Bogdanescu

Founder & Senior Architect

Retrieving relationships of relationships using Eloquent in Laravel

When working with nested associations in Laravel, it is easy to assume that calling with() multiple times on a model instance will automatically hydrate deep chains of relationships. If you have an Advert that belongs to a Car, which belongs to a Model, the syntax Advert::find(1)->with('Car')->with('Model') is intuitive but incorrect. This approach fails because the first call returns a fully hydrated model instance, not a query builder object. Once Eloquent has instantiated the Advert, you cannot chain eager loading methods to fetch its related data; those methods only function on the Query\Builder interface.

To retrieve the details of the Model when accessing an Advert, you must specify the entire relationship path in a single with() call. Eloquent supports dot notation for nested relationships, allowing you to load the full chain in one query execution. The correct syntax is:

$advert = Advert::find(1)->with('Car.Model')->first();

By passing 'Car.Model' as the argument, Eloquent understands that you want to eagerly load the Model through the intermediate Car relationship. It will generate a single SQL query that utilizes an INNER JOIN or LEFT JOIN depending on your relationship definitions (typically many-to-one relationships like Car and Model). This ensures optimal performance by avoiding the N+1 query problem that would occur if you were to fetch these records lazily.

Understanding the Relationship Paths

In your specific schema, the path is defined as Advert → Car → Model. When using dot notation, the string passed to with() must represent the exact sequence of relationship names defined on each model class.

If your models are structured as follows:

  • The Advert model has a $belongsTo or $hasOne relationship named car.
  • The Car model has a $belongsTo relationship named model.

The eager loading string must reflect these specific method names. If you defined the relationships using different keys, such as advertedCar on the Advert model or parentModel on the Car model, the path would need to be updated accordingly:

$advert = Advert::find(1)->with('car.parentModel')->first();

Accessing the Nested Data

Once the eager loading is complete, accessing the nested data remains straightforward. You can traverse the relationships directly on the instance because Eloquent has already fetched and populated them.

$modelName = $advert->car->model->name;

Even though you loaded the Model through the Car relationship chain, the resulting object graph is fully hydrated. You do not need to make additional calls to load() or run separate queries. This approach is significantly more efficient than retrieving the Advert first and then calling $advert->car followed by $advert->car->model without eager loading, which would trigger separate database hits for every advert retrieved in a loop.

Limitations and Considerations

While dot notation simplifies deep relationships, it does have boundaries. You can only navigate as far as the number of levels defined in your schema. Attempting to load a relationship that does not exist on the intermediate model will throw an error. Additionally, while Eloquent handles up to several levels of nesting gracefully, extremely deep chains (e.g., A.B.C.D.E) can sometimes lead to complex SQL join conditions that might be harder to debug than breaking them into separate eager loads if necessary.

There are scenarios where you might need to load relationships conditionally or with specific constraints at different levels of the hierarchy. In those cases, Eloquent allows passing arguments directly in the with() method, though this becomes more complex as nesting increases. For instance, if you need to filter models based on a condition within the deep chain, you would typically use the loadWithConstraints pattern or separate queries for deeper specificity.

For further reading on advanced relationship loading strategies and optimizing query performance in Laravel, resources from https://laravelcompany.com provide detailed insights into building scalable applications with these patterns.

Our network