DEV Community

Paul Edward
Paul Edward

Posted on

Laravel Optional Helper

Laravel Optional Helper

_# The “optional” function can accept any argument which will allow 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
_
I saw a post on twitter by Taylor Otwell on how he use Laravel Optional helper

i said to myself, alot might not be aware of the helper method called Optional, so i decided to give it some voice on my blog today

The optional method started with laravel 5.5 and i am going to show you how i have been using it just to clear up with my views

First and foremost install laravel application starting from 5.5 upward and generate authentication scaffolding with

php artisan make:auth

Register and account and sign in and as you can see the below the usual laravel auth scaffolding

Now head over to your command line and let’s make an Profile model with a one to one relationship with the User Model. For example a user with one profile, so i generating this along with the migration

$ php artisan make:model Profile -m

go over to the Profile Migration in the database folder and add the following details

*public function *up()
{
    Schema::create('profiles', *function *(Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('user_id')->index();
        $table->string('address');
        $table->timestamps();
    });
}
Enter fullscreen mode Exit fullscreen mode

once done, you can migrate using the php artisan migrate

Just for the purpose of the tutorial, i will have to manually fill in the address details in the Profile table, relating it to my Id in the user table

less i forget, it actually skipped me, we need to add the one to one relationship in the user model (User.php)

*public function *profile(){
    return $this->hasOne(Profile::*class*);
}
Enter fullscreen mode Exit fullscreen mode

so over in my home.blade.php, i want to go ahead and output my address on the dashboard

outputting the user address using the auth helper function in relation to the profile actually works using this

{{ auth()->user()->profile->address }}
Enter fullscreen mode Exit fullscreen mode

What if the user doesn’t have an address. to demonstrate this i delete the record i created in the profile table

I did and i refreshed my dashboard, as you rightly guess, there is going to be an error because we are trying to get a property of a non object, because the profile relationship is returning NULL and actually that is what i got

in solving this previously, what would have been done is to use an IF statement and definately that will fix the problem

*@if(*auth()->user()->profile*)
    *{{ auth()->user()->profile->address }}
*@endif*
Enter fullscreen mode Exit fullscreen mode

The only issue with this is wrapping this within an IF statement and just for the purpose of this tutorial and to lay the foundation and usefulness of optional helper, so we will be changing the above code

so, we take what we are trying to access then wrap it inside the optional helper and that works

{{ optional(auth()->user()->profile)->address }}
Enter fullscreen mode Exit fullscreen mode

the optional helper will introduce a dynamic getter on whatever we passed into it and that means if the property we are trying to access doesn’t exist, it will silently return null

and that works as well, so let go ahead and add an address back into the Profile table to be sure everything is fine

and yes it works

So if you are wrapping loads of IF then the optional helper is really going to help you out and of course you can use this absolutely anywhere and not just limited to views,

in conclusion you can use it to tidy up your code. we also have the Optional() callback. i will be writing about it soon in case you need it.

Happy Coding

Top comments (0)