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.
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 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
- Use
$post->user?->nameoroptional($post->user)->name - Add
->withDefault()in thebelongsTorelation - Validate that the record exists (
firstOrFail)
Detailed fix by platform
Laravel
public function user() { return $this->belongsTo(User::class)->withDefault(['name' => 'Guest']); }
How to diagnose
- Value — Null where?
- Relation — Row exists?
- 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:
Similar errors
- 403 This action is unauthorized Laravel: Illuminate\Auth\Access\AuthorizationException: This action is unauthorized.
- 419 Page Expired Laravel: 419 Page Expired (CSRF token mismatch)
- 422 Unprocessable Entity: The given data was invalid Laravel: {"message":"The given data was invalid.","errors":{"email":["The email field is required."]}} (422)
Most viewed in Laravel
- MassAssignmentException Laravel: Illuminate\Database\Eloquent\MassAssignmentException: Add [x] to fillable property to allow mass assignment
- The GET method is not supported for route Laravel: The GET method is not supported for route x. Supported methods: POST (MethodNotAllowedHttpException)
- Route [login] not defined Laravel: Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 4 Nov 2026