Accessing Eloquent relationship attributes
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering Nested Eloquent Relationships: Accessing Attributes Across Multiple Models
As developers working with relational databases through an ORM like Eloquent, navigating complex, multi-level relationships is a common task. When you set up one-to-many relationships across several models—like your Category, Subcategory, and Style example—it’s natural to expect seamless access to deeply nested data. However, as you've discovered, accessing these attributes can sometimes lead to frustrating errors like "Trying to get property of non-object."
This post will dissect why this happens, clarify the mechanics of Eloquent relationships, and show you the definitive best practices for efficiently retrieving and accessing related model instances.
## The Pitfall of Lazy Loading and Relationship Access
The issue you are encountering stems from how Eloquent handles lazy loading and the context in which you attempt to access related data. When you define a `belongsTo` relationship (like `Style` belongs to `Subcategory`), Eloquent sets up a mechanism to fetch the parent record based on the foreign key.
When you call `$style->subcategory`, if the necessary subcategory hasn't been loaded into memory yet, or if the context doesn't provide sufficient information for immediate retrieval, Eloquent might return only the foreign key ID (an integer) instead of the fully instantiated model object, leading