Attempt to read property "name" on null

Attempt to read property "name" on null

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

Stefan Bogdanescu

Founder & Senior Architect

Attempt to read property "name" on null

Encountering the Illuminate\Support\Str::replace-style error "Attempt to read property 'name' on null" is a classic symptom of a mismatch between your database schema and your Eloquent relationships. In this specific scenario, you have defined a belongsTo relationship in the News model pointing to Category, but accessing $news->category->name fails because the category relationship returns null for every record.

The root cause lies in how Laravel handles eager loading versus direct database constraints. While your migration correctly defines the foreign key:

$table->foreign('cat_id')->references('id')->on('categories')->onDelete('cascade');

and your controller loads the relationship using $news->with('category'), the error suggests that the categories table is either missing data or the relationship definition has a subtle misalignment. However, in many cases, this specific error appears when the eager loading hasn't actually populated the relation because of how the collection is iterated or if the foreign key values don't match existing primary keys exactly (e.g., type casting issues).

But there is a more insidious possibility often overlooked: Mass Assignment Protection and Fillable Attributes.

The fill vs. fillable Trap

In your Category model, you have defined:

protected $fillable = ['name', 'number'];

This looks correct for reading data. However, if you ever run into issues where the relationship seems broken after an update or create operation, it is often due to how the parent record was saved. If the Category model itself wasn't properly instantiated with the $name attribute when the relationship was established, accessing it later will yield null.

More critically, look at your News model's category relationship:

public function category() {
    return $this->belongsTo(Category::class);
}

This is syntactically correct. The issue usually manifests if the cat_id in the news table contains values that do not exist in the categories table (orphan records). If your foreign key constraint allows nulls or if you are manually inserting data without checking for the existence of the category, $news->category will naturally return null.

Debugging the Null Relationship

Before assuming a schema error, verify what is actually being loaded. The with('category') syntax tells Eloquent to eager load the relationship. If the resulting collection shows $news->category as null, it implies one of two things:

  1. The cat_id stored in the news table does not match any id in the categories table.
  2. There is a caching issue where Eloquent thinks the relationship hasn't changed, though this is less common with fresh queries.

To debug this without editing the Blade file repeatedly, you can inspect the raw data or use a temporary dump in your controller:

public function news_index() {
    // Check if categories exist and match IDs
    $categories = Category::pluck('id', 'name'); 
    
    $news = News::with('category')->get();
    
    foreach ($news as $item) {
        if (!$item->category && $item->cat_id !== null) {
            // This indicates an orphan record
            \Log::warning("Orphan news found: {$item->title} references non-existent category {$item->cat_id}");
        }
    }

    return view('News.list', compact('news'));
}

If you find that cat_id exists but points to no category, your migration logic or initial data seeding might need adjustment.

Handling the Missing Relationship Safely

Even if the data is correct, rendering a property on a nullable relationship can crash your application if the user accesses a row where the category is missing. A robust Blade template should handle this gracefully rather than throwing an unhandled exception.

Modify your news-list.blade.php to use the null coalescing operator or a conditional check:

<td> 
    {{ $new->category->name ?? 'Uncategorized' }}
</td>

This prevents the application from halting if a category is missing. It also gives you an immediate clue in production logs if "Uncategorized" appears frequently, indicating data integrity issues that need fixing in your seeding or migration scripts.

Optimizing Eager Loading with Constraints

If you are certain the categories exist but eager loading still feels unreliable, ensure that the Category model isn't being restricted by its own scope or fillable rules during the join. Sometimes, if a model uses $appends or specific accessors that rely on data not present in the joined table, it can cause confusion.

However, given your setup, the most likely fix involves ensuring that every cat_id inserted into the news table corresponds to an actual record in the categories table. If you are using a form to create news items, ensure that the dropdown for categories is populated from an existing list and not hardcoded with IDs that might have been deleted.

For more complex relationship debugging, consider visiting https://laravelcompany.com if you need deeper insights into optimizing your Eloquent queries or handling nested relationships with custom constraints. But for now, verifying data integrity between the news and categories tables is the definitive step to resolve the "property name on null" error.

Our network