Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ryangurnick's avatar

Eloquent Nullsafe Operator

Does anyone happen to know if eloquent supports the null safe operator?

The reason I ask if mainly due to an excessive amount of error checking needed to traverse more than one relationship when calling a model, it would be amazing if I could just use ?-> and check for a single null value.

My hope is that if this is not supported now that it will be shortly, but seeing as how laravel generally supports new php standards before I even know they exist, I am hoping the simple answer of YES comes from this thread.

I have not been able to find anything about this in the laravel documentation, hence the question.

0 likes
7 replies
thinkverse's avatar

Yes, you can use the nullsafe operator as long as your Laravel application is running PHP 8.0.0+, it is a core PHP operator as of 8.0.0 so. Laravel doesn't change your PHP binary and remove operators, they're built-in on the language level.

There could be some new features in PHP that Laravel might not support in the beginning, but that if anything is mostly new functions, new classes, or new types that aren't type-hinted. When it comes to the nullsafe operator though that's language syntax that doesn't need a class or specific type to work.

ryangurnick's avatar

@thinkverse That is kind of what I guessed, the main reason I was asking is because the php documentation makes it seem like you need to include special operators in the base classes, but I appreciate the insight.

thinkverse's avatar

php documentation makes it seem like you need to include special operators in the base classes

@ryangurnick I don't get the impression it needed to include a special operator in the base class. Where does it give of that impression? The nullsafe operator is the same as the object operator, apart from it begins with a question mark. ?-> over ->.

ryangurnick's avatar

@thinkverse Truthfully, I cannot remember, I was looking into this late at night so I quite likely was mistaken.

kokoshneta's avatar
Level 27

Well… yes and no.

In general, yes, as @thinkverse says, the null-safe operator is a core PHP feature, so Eloquent ‘supports’ it just as much as it ‘supports’ == or >> or %.

But there are some extra considerations with Laravel collections (including Eloquent collections), which have a lot of method magic built into them. The same limitations that apply to the null-coalescing operator ?? also apply to the null-safe operator.

Specifically, attempting to access a nonexistent property on a collection will throw an exception rather than returning NULL (as it would with a model or any other regular PHP object), so null-coalescence or null-safety is no help. Assuming you have fetched a user and have a working posts relationship on your user model, the first two examples in the following will work as expected, whereas the last two will throw an exception:

$user_thingy = $user->thingy ?? 'a'; // → 'a'
$user_thingy = $user?->thingy; // → NULL

$post_thingy = $user->posts->thingy ?? 'a'; // throws exception
$post_thingy = $user->posts?->thingy; // throws exception

Note that $thingy in the last two examples is treated as a property of the collection itself, not a property of each post in the collection. The collection has no property called $thingy, so it looks instead for a higher-level function with that name; when it doesn’t find one, it throws an exception, before the null-coalescing or null-safe operator is even reached.

2 likes

Please or to participate in this conversation.