The error "Call to a member function on null" is one of the most common PHP and Laravel errors.
It happens when your code tries to call a method on a variable that does not contain an object.
Example error:
Fatal error: Call to a member function something() on nullor in Laravel:
Call to a member function save() on nullThis means PHP expected an object, but the value was actually null.
In this guide, you will learn why this happens and how to fix it in Laravel applications.
Why Does "Call to a Member Function on Null" Happen?
A simple example:
$user = User::find(10); $user->profile->update([ 'name' => 'Ali' ]);If the user does not have a profile, then:
$user->profilereturns:
nullThen PHP tries:
null->update()which causes:
Call to a member function update() on nullFix 1: Check If the Object Exists Before Calling the Method
Before calling a method, verify the value exists.
Example:
$user = User::find(10); if ($user) { $user->update([ 'name' => 'Ali' ]); }This prevents PHP from calling a method on a null value.
Fix 2: Use Laravel Optional Helper
Laravel provides the optional() helper.
Instead of:
$user->profile->name;use:
optional($user->profile)->name;If the profile does not exist, Laravel returns null instead of throwing an error.
Fix 3: Use Null Safe Operator in PHP 8+
If your server uses PHP 8 or newer:
$user?->profile?->update([ 'name' => 'Ali' ]);The ?-> operator checks if the object exists before continuing.
Fix 4: Check Laravel Model Relationships
A very common Laravel cause is missing relationships.
Example:
User model:
public function profile() { return $this->hasOne(Profile::class); }Controller:
$user->profile->address;If no profile record exists:
$user->profilereturns null.
Fix:
$user->profile()->create([ 'address' => 'London' ]);or check first:
if ($user->profile) { echo $user->profile->address; }Fix 5: Check find() and first() Results
Laravel methods like:
User::find($id);can return null.
Example:
$user = User::find(999); $user->name;If user ID 999 does not exist:
$user = null;Fix:
Use this instead:
$user = User::findOrFail($id);Now Laravel will return a 404 response instead of a null error.
Fix 6: Check Authentication User
Another common Laravel issue:
auth()->user()->name;If nobody is logged in:
auth()->user()returns null.
Fix:
auth()->user()?->name;or:
if(auth()->check()){ echo auth()->user()->name; }Fix 7: Check API Responses
When working with APIs:
$response->data->name;can fail if the API does not return data.
Check first:
if($response->data){ echo $response->data->name; }Fix 8: Clear Laravel Cache After Changes
Sometimes the code is fixed but old cached configuration causes issues.
Run:
php artisan optimize:clearThen:
php artisan optimizeHow to Find the Exact Line Causing the Error
Check your Laravel log:
storage/logs/laravel.logYou will see something like:
ErrorException: Call to a member function update() on null app/Http/Controllers/UserController.php:45The file and line number tell you where the problem is.
Common Laravel Examples
Example: Null User
Problem:
$user = User::where('email',$email)->first(); $user->delete();Fix:
$user = User::where('email',$email)->first(); if($user){ $user->delete(); }
Example: Null Relationship
Problem:
$order->customer->email;Fix:
$order->customer?->email;
Example: Null Model
Problem:
$post = Post::find($id); $post->save();Fix:
$post = Post::findOrFail($id); $post->save();Final Checklist
When you see:
Call to a member function on null
Check:
✓ Is your model returning null?
✓ Does your database record exist?
✓ Is the relationship created?
✓ Is the authenticated user available?
✓ Are you checking API responses?
✓ Did you clear Laravel cache?
Most Laravel null errors happen because your code assumes data exists when it does not.
If your Laravel application is showing this error and you cannot find the cause, send me the error message and stack trace and I can help debug it.