PHP: Call to a member function on null - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

PHP: Call to a member function on null
======================================

### [\#PHP](https://rocketeersapp.com/knowledge/php)

This error means you called a method on a variable that turned out to be null. The variable you expected to hold an object was empty, often a database lookup that found nothing.

 Published by [Mark van Eijk](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [About the error](#content-about-the-error)
2. [Why do I see this error](#content-why-do-i-see-this-error)
3. [Solution](#content-solution)
4. [Find the null](#content-find-the-null)
5. [Handle the empty case](#content-handle-the-empty-case)
6. [Relationships](#content-relationships)

[\#](#content-about-the-error "Permalink")About the error
---------------------------------------------------------

 ```
Error: Call to a member function format() on null

```

You called a method (`->format()`, `->name`, `->save()`) on something that was `null` instead of the object you expected. PHP can't call a method on nothing, so it throws. In a web request this reaches visitors as a [500 Internal Server Error](/500-internal-server-error-laravel).

The method name in the message is a strong clue: it tells you *which* object was missing.

[\#](#content-why-do-i-see-this-error "Permalink")Why do I see this error
-------------------------------------------------------------------------

- A database query returned no row, so the model is `null`.
- A relationship isn't loaded or doesn't exist for this record.
- An optional value (a nullable column, a missing config key) is genuinely empty.
- A function you assumed always returns an object returned `null` on failure.

[\#](#content-solution "Permalink")Solution
-------------------------------------------

### [\#](#content-find-the-null "Permalink")Find the null

In Laravel, `find()` and `first()` return `null` when nothing matches. Calling a method on that result fails:

 ```
$user = User::find($id);   // null if no such user
echo $user->name;          // Call to a member function on null

```

### [\#](#content-handle-the-empty-case "Permalink")Handle the empty case

Decide what should happen when it's missing. To 404 automatically, use `findOrFail()`:

 ```
$user = User::findOrFail($id);   // throws a 404 instead of returning null

```

To provide a fallback, check first or use the nullsafe operator (PHP 8+):

 ```
// guard explicitly
if ($user) {
    echo $user->name;
}

// or the nullsafe operator: returns null instead of erroring
echo $user?->profile?->bio ?? 'No bio';

```

### [\#](#content-relationships "Permalink")Relationships

A relationship that hasn't been set returns `null` too. The nullsafe operator and `optional()` helper both guard against it:

 ```
$company = $user->company?->name ?? 'Independent';

```

The fix is rarely to suppress the error, it's to decide deliberately what the empty case should do. A related strictness error is the [undefined array key](/undefined-array-key-php) warning in PHP 8.

### Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#PHP](https://rocketeersapp.com/knowledge/php)

- [How to run PHP files](https://rocketeersapp.com/knowledge/run-php-files)
- [PHP file upload exceeds upload\_max\_filesize](https://rocketeersapp.com/knowledge/php-file-upload-exceeds-upload-max-filesize)
- [PHP Warning: Undefined array key](https://rocketeersapp.com/knowledge/undefined-array-key-php)
- [PHP Fatal error: Allowed memory size exhausted](https://rocketeersapp.com/knowledge/php-allowed-memory-size-exhausted)
- [PHP Fatal error: Class "X" not found](https://rocketeersapp.com/knowledge/php-class-not-found)
- [Composer out of memory (allowed memory size exhausted)](https://rocketeersapp.com/knowledge/composer-out-of-memory-allowed-memory-size-exhausted)

 [View all 11 articles →](https://rocketeersapp.com/knowledge/php)
