Echo value in Laravel Blade

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Mastering Nested Data Access in Laravel Blade: Avoiding the "Trying to get property of non-object" Error As developers working with complex applications built on frameworks like Laravel, navigating nested data structures is a daily task. When you pull data from your database via Eloquent models, you often end up dealing with deeply nested relationships. However, accessing these properties in Blade templates can easily lead to frustrating runtime errors, especially when that data structure isn't perfectly consistent. This post will dive into why you are encountering the `ErrorException Trying to get property of non-object` error when trying to access nested values like `$vestiging->rekenplichtige->name`, and provide robust, modern solutions for safely handling this data in your Laravel Blade templates. ## The Root Cause: Why Nested Access Fails The error you are seeing stems from a fundamental principle of object-oriented programming: you can only access properties (methods) if the object they belong to actually exists. When you write: ```blade {{ $vestiging->rekenplichtige->name }} ``` PHP attempts to follow this path: 1. Find `$vestiging`. 2. Check if `$vestiging` is an object. (Assume yes.) 3. Try to access the property `rekenplichtige` on `$vestiging`. 4. Check if the result of step 3 (i.e., `$vestiging->rekenplichtige`) is an object. If, for any reason—perhaps a database record was missing a required relationship, or a specific query returned `null` for that relationship—the intermediate object (`$vestiging->rekenplichtige`) resolves to `null`, the next attempt to access a property on it (`->name`) throws the fatal error: **"Trying to get property of non-object."** This is especially common when dealing with Eloquent relationships. If you haven't explicitly defined the relationship or if the related record does not exist, Laravel might return `null` instead of an empty object, leading to this crash in the view layer. ## Safe Access Strategies for Blade Templates The key to solving this is to implement defensive programming—checking for existence *before* attempting access. As a senior developer, I always advocate for methods that handle potential nulls gracefully. ### 1. The Traditional Guard: Explicit Conditional Checks The most reliable and widely compatible method involves explicitly checking if the parent object exists before proceeding. This ensures your code remains robust regardless of the data state. ```blade @php // Check if both parent objects exist before trying to access the final property $name = null; if (isset($vestiging) && $vestiging->rekenplichtige) { $name = $vestiging->rekenplichtige->name; } @endphp

Name: {{ $name ?? 'N/A' }}

``` While this works perfectly, it can become verbose if you have many nested properties. ### 2. The Modern Approach: Nullsafe Operator (PHP 8+) If your project is running on PHP 8 or newer (which is standard for modern Laravel installations), the Nullsafe Operator (`?->`) provides a concise and elegant way to safely navigate object chains. If any part of the chain is `null`, the entire expression immediately short-circuits and returns `null` instead of throwing an error. ```blade {{ $vestiging?->rekenplichtige?->name ?? 'Data Not Found' }} ``` **Explanation:** This single line attempts to access `rekenplichtige` only if `$vestiging` exists. If `$vestiging` is null, it stops and returns null immediately. Then, it tries to access `name` on the result. If `rekenplichtige` was null, the expression returns null instead of crashing. We use the Null Coalescing Operator (`??`) at the end to provide a default fallback value if the entire chain fails. ### 3. Leveraging Eloquent for Data Integrity Before reaching the Blade layer, always ensure your Eloquent models are set up correctly. If you are dealing with relationships, make sure you are using proper relationship definitions and that you handle potential missing data in your controller or service layer. Good data handling starts at the source. For robust data management within Laravel, understanding how Eloquent manages these relationships is crucial, as detailed in guides on [Laravel Company](https://laravelcompany.com). ## Conclusion The error `Trying to get property of non-object` is a clear signal that your template code is not accounting for potential null values in your nested data. By switching from direct access (`->`) to defensive checks (like the Nullsafe Operator or explicit `isset()`), you transform brittle code into resilient, production-ready logic. Always prioritize safety when dealing with external or database-driven data, ensuring that your Laravel applications remain stable and predictable.