phplaravel

How does Laravel optional() work?


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?


Solution

  • 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.