Attempt to read property "x" on null Laravel Origin: Application & API OSI layer 7 · Application Last updated 4 Nov 2026

Laravel/PHP 8: ErrorException: Attempt to read property "name" on null

You accessed a property on a value that is null, typically an empty relationship or a query that found nothing.

Exact message you may see Attempt to read property "name" on nullTrying to get property 'name' of non-objectErrorException: Attempt to read property on nullCall to a member function format() on nullCall to a member function x() on null Laravel
Seen on: Laravel PHP

Meaning

$post->user->name fails if user is null (no related row or soft-deleted). Use the null-safe operator $post->user?->name, optional(), eager-load with default models (withDefault()), or check existence. In Blade use {{ $post->user?->name }}.

Common causes

  • Relationship returns null (no related row)
  • first() found no record
  • Soft-deleted parent
  • Eager loading missing key
  • Wrong foreign key in relation

⚡ Quick fix

  1. Use $post->user?->name or optional($post->user)->name
  2. Add ->withDefault() in the belongsTo relation
  3. Validate that the record exists (firstOrFail)

Detailed fix by platform

Laravel

  1. public function user() { return $this->belongsTo(User::class)->withDefault(['name' => 'Guest']); }

How to diagnose

  1. Value — Null where?
  2. Relation — Row exists?
  3. Guard — ?-> used?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

403 This action is unauthorized — Laravel: Illuminate\Auth\Access\AuthorizationException: This action is unauthorized. (Laravel)403 This action is un…419 Page Expired — Laravel: 419 Page Expired (CSRF token mismatch) (PHP)419 Page Expired422 Unprocessable Entity: The given data was invalid — Laravel: {"message":"The given data was invalid.","errors":{"email":["The email field is required."]}} (422) (Laravel)422 Unprocessable Ent…429 Too Many Requests (Laravel throttle) — Laravel: Illuminate\Http\Exceptions\ThrottleRequestsException: Too Many Attempts. / 429 Too Many Requests (Laravel)429 Too Many Requests…BadMethodCallException: Call to undefined method — Laravel: BadMethodCallException: Call to undefined method App\Models\Post::whereTitle() / Illuminate\Database\Eloquent\Builder::foo() (Laravel)BadMethodCallExceptio…Base table or view already exists — Laravel: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (Laravel)Base table or view al…Class "App\Models\X" not found — Laravel: Error: Class "App\Models\Post" not found (Laravel)Class "App\Models\X" …Command "x" is not defined — Laravel: Symfony\Component\Console\Exception\CommandNotFoundException: Command "make:livewire" is not defined. Did you mean one of these? (Laravel)Command "x" is not de…Attempt t…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.