Trying to get property of non-object - Laravel 5
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Troubleshooting "Trying to get property of non-object" Error in Laravel 5 Applications
Body:
One common pain point that developers face is when they encounter the frustrating `ErrorException: Trying to get property of non-object` error in their Laravel 5 applications. This issue often occurs when trying to reference a relationship or a specific property from an object that doesn't exist or lacks a certain attribute. In this comprehensive blog post, we will dive into some common reasons for this error and provide practical solutions.
Firstly, let us understand the code provided in the given example:
1. News model: Here, we have defined the relationship between a news article and its corresponding user by creating a `postedBy()` method. This way, if one requires to get details about the user who posted the specific news article, they can do so by calling the method.
2. User model: This model represents the users of our Laravel application. We have used standard authentication and authorization functionalities provided by Laravel through implementing `AuthenticatableContract`, `AuthorizableContract`, and `CanResetPasswordContract` interfaces.
3. Controller: The controller is responsible for handling HTTP requests, including fetching specific articles based on their slug. It's also responsible for returning the appropriate view with the requested article data passed as a parameter (here compact('article')).
4. Blade template: In this case, we have attempted to access user information by calling `$article->postedBy->name` in the blade view. However, this led to the error mentioned, due to issues related to object properties and relationships.
Now, let's analyze the possible reasons for this error:
1. Non-existent relationship: If there is no corresponding user attached to a particular news article (i.e., the `postedBy` method returns null), attempting to access the name via the relationship will result in the error. In such cases, you should ensure that every news article has an associated user.
2. Missing or incorrect foreign key: If there is a mismatch between the foreign keys or relationships defined in your models and their corresponding tables, this may cause issues with retrieving the user information. To fix this, verify that both the `users` table's `id` field and the news model's `postedBy_id` belong to the same data type and are correctly set up in your application.
3. Incorrect model instances: A common mistake is using the wrong instance of a model when calling relationships, leading to unexpected errors or behavior. Always ensure that you are accessing the correct models and their properties without any issues. If possible, use Laravel's `with()` method to eager-load the required relationship information alongside your query results.
4. Missing relationships in the migration: Occasionally, relationships may not be defined properly in migrations. Check if you have added all necessary columns and relationships for both user and news models without forgetting any constraints or indexes. Additionally, ensure that the correct database is connected to your Laravel application, as this could also cause issues with the data retrieval process.
Now let's move on to fixing these errors:
1. Ensure each article has a valid `postedBy_id` in the news table (foreign key).
2. Check that both tables have their foreign keys defined properly, and that they correspond to one another.
3. Always use the correct model instances when accessing relationships or properties - preferably, use Eloquent's built-in methods like `belongsTo()` instead of manually creating them.
4. If the error persists, double-check your migrations and database configuration settings to ensure no discrepancies exist between your models and their corresponding tables.
In conclusion, troubleshooting the "Trying to get property of non-object" error in Laravel applications requires a systematic approach, starting with identifying the root cause and applying appropriate solutions. By following best practices when using relationships and ensuring correct model instances and database configurations, you can prevent such issues from arising in your Laravel 5 projects.