Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Laravel optional() work?

Tags:

php

laravel

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?

like image 314
Fredrik Avatar asked Apr 20 '18 08:04

Fredrik


People also ask

What is :: In laravel?

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 helper function in laravel?

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.


2 Answers

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.

like image 163
Asur Avatar answered Oct 01 '22 03:10

Asur


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;
like image 26
Sasha MaximAL Avatar answered Oct 01 '22 02:10

Sasha MaximAL