When creating new models, I am trying to use the optional()
helper to assign values when they are set. However, when the user
object isn't set in this context, I receive this error: "Undefined property: stdClass::$user"
$this->events()->create([
'category_a' => optional($event)->categoryA,
'category_b' => optional($event)->categoryB,
'user' => optional($event->user)->accountId,
]);
To clarify, the $event
is always set, however it can contain different values. Sometimes category_a
is set, sometimes category_b
is set. The optional helper seems to do the trick here. But, when the object is deeper than one level, it throws an error.
So how do I work with the optional
helper correctly using deeper than one level objects?
Basically its know as Scope resolution operator (::) Simply it is token which allow access to static, constant and overridden properties of method of a class. Example- in laravel we call model like this.
What is a Helper Function? A helper function usually comes in handy if you want to add a feature to your Laravel application that can be used at multiple locations within your controllers, views, or model files. These functions can be considered as global functions.
The optional()
helper normally is used to avoid errors generated by accesing propeties or methods on null objects, for example:
Fatal error: Call to a member function foo() on null
As the documentation specifies:
The optional function accepts any argument and allows you to access properties or call methods on that object.If the given object is null, properties and methods will return null instead of causing an error.
A practical example
In your code you are accesing a relation called address
from your user
model:
return $user->address->street;
If by any chance your $user->address
is null and you try to check the street
, this will return a fatal error, here is where optional()
comes into play, by using the helper you can explicitly say $user->address
might be null, so don't show me an error if that's the case:
return optional($user->address)->street;
By doing this you will get null
instead of the fatal error.
In my opinion this makes the code more readable, a equivalent could be $user->address ? $user->address->street : null
, but as you can see is by far more verbose.
In your case, as the comments said an option is to nest optional()
helpers, but this is not maintanable if you have multiple levels. I'd recommend you to make a function that internally will chain the helpers so your syntax would look like n_optional($event, ['user'])->accountId
.
Hope this helps you.
Also, bear in mind that in PHP 8+ you can use the null safe operator to have the same behavior with cleaner look:
// PHP 8+
$this->events()->create([
'category_a' => $event?->categoryA,
'category_b' => $event?->categoryB,
'user' => $event->user?->accountId,
]);
It is also chainable and can be used with methods:
// before PHP 8
$country = null;
if ($session !== null) {
$user = $session->user;
if ($user !== null) {
$address = $user->getAddress();
if ($address !== null) {
$country = $address->country;
}
}
}
// PHP 8+
$country = $session?->user?->getAddress()?->country;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With