Attempt to read property "name" on null Laravel 9
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Solving the Dreaded Error: Attempt to Read Property "name" on Null in Laravel Eloquent
As developers building complex applications with Laravel and Eloquent, we frequently encounter errors that seem trivial but cause significant headaches. One of the most common culprits is the dreaded Attempt to read property "name" on null. This error signals that you are attempting to access a property on an object reference that currently holds the value null, usually because a database lookup failed or a relationship was not properly loaded.
This post dives deep into the specific scenario you described—creating relational data (Products, Categories, Brands) and trying to access nested names—and provides a comprehensive, developer-friendly solution using best practices from Laravel.
Understanding the Root Cause: Why Does This Happen?
The error Attempt to read property "name" on null occurs when your Eloquent chain breaks at an intermediate step. In your inventory system scenario, this most likely happens in the view layer when you execute code like {{ $item->category->name }}.
Here is the breakdown of where the failure likely originates:
- Missing Relationship Loading: You define relationships (
belongsTo,hasMany), but by default, Eloquent does not load related models unless explicitly told to do so. When you fetch aProductmodel, it only contains its own data; it doesn't automatically include the full details of the relatedCategory. - Foreign Key Mismatch (The Deeper Issue): Even if you load the relationship, if the foreign key in your
productstable (cat_id) points to a category that does not exist in thecategoriestable, Eloquent will returnnullfor that relationship instead of throwing a fatal database error. When you then try to access$item->category->name, you are attempting to read a property onnull, resulting in the error.
Solution 1: Mastering Eager Loading with with()
The most efficient and idiomatic way to solve N+1 query problems and ensure all related data is present is by using Eager Loading via the with() method. This tells Eloquent to fetch the necessary related data in a single, optimized query, rather than executing separate queries for every product.
In your controller, you should modify how you retrieve the products:
// ProductController.php
public function index(Request $request)
{
$keyword = $request->get('search');
$perPage = 25;
$products = Product::with('category', 'brand') // <-- Eager load relationships here!
->where('barcode_no', 'LIKE', "%{$keyword}%")
->orWhere('productname', 'LIKE', "%{$keyword}%")
->orWhere('cat_id', 'LIKE', "%{$keyword}%")
->orWhere('brand_id', 'LIKE', "%{$keyword}%")
->orWhere('price', 'LIKE', "%{$keyword}%")
->latest()
->paginate($perPage);
return view('admin.products.index', compact('products'));
}
By adding ->with('category', 'brand'), Eloquent executes the necessary JOIN operations internally (or separate queries, depending on configuration) to fetch all related category and brand data simultaneously. This ensures that when you iterate over $products, the $item->category relationship is already populated with the actual Category model instance, thus preventing the null reference error. This principle of efficient data retrieval is central to building performant applications, much like adhering to the principles outlined by the Laravel Company.
Solution 2: Defensive Coding with Null Coalescing
Even with eager loading, robust code anticipates potential failures. If there's a possibility that a relationship might legitimately be missing (e.g., a product has a null category ID), we must add defensive checks to prevent runtime errors in the view layer.
In your Blade view file (products/index.blade.php), use the Nullsafe Operator (?->) or explicit conditional checks:
Using Nullsafe Operator (PHP 8+):
If you are using PHP 8+, this is the cleanest approach:
@foreach($products as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->barcode_no }}</td>
<td>{{ $item->productname }}</td>
{{-- Use ?-> to safely access nested properties --}}
<td>{{ $item->category?->name ?? 'N/A' }}</td>
<td>{{ $item->brand?->name ?? 'N/A' }}</td>
</tr>
@endforeach
Using Traditional Conditional Checks:
For broader compatibility, you can use standard checks:
@foreach($products as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->barcode_no }}</td>
<td>{{ $item->productname }}</td>
<?php if ($item->category) { ?>
<td>{{ $item->category->name }}</td>
<?php } else { ?>
<td>Category Missing</td>
<?php } ?>
<?php if ($item->brand) { ?>
<td>{{ $item->brand->name }}</td>
<?php } else { ?>
<td>Brand Missing</td>
<?php } ?>
</tr>
@endforeach
Conclusion
The "Attempt to read property on null" error is a classic symptom of incomplete data loading in Eloquent relationships. By mastering Eager Loading using with('relationship_name') in your controller, you ensure that related models are fetched efficiently. Furthermore, by implementing defensive coding techniques like the Nullsafe Operator (?->) in your views, you make your application resilient to imperfect or missing data. Always aim for efficiency and robustness when dealing with database interactions; this focus on clean, reliable data handling is a hallmark of high-quality Laravel development, aligning perfectly with the standards promoted by the Laravel Company.