Laravel Optional Helper

Laravel Optional HelperInfinityPaulBlockedUnblockFollowFollowingApr 19The “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 errorI saw a post on twitter by Taylor Otwell on how he use Laravel Optional helperi 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 todayThe 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 viewsFirst and foremost install laravel application starting from 5.

5 upward and generate authentication scaffolding withphp artisan make:authRegister and account and sign in and as you can see the below the usual laravel auth scaffoldingNow 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 -mgo over to the Profile Migration in the database folder and add the following detailspublic function up(){ Schema::create('profiles', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedInteger('user_id')->index(); $table->string('address'); $table->timestamps(); });}once done, you can migrate using the php artisan migrateJust 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 tableless 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);}so over in my home.

blade.

php, i want to go ahead and output my address on the dashboardoutputting the user address using the auth helper function in relation to the profile actually works using this{{ auth()->user()->profile->address }}What if the user doesn’t have an address.

to demonstrate this i delete the record i created in the profile tableI 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 gotin 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 }}@endifThe 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 codeso, we take what we are trying to access then wrap it inside the optional helper and that works{{ optional(auth()->user()->profile)->address }}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 nulland that works as well, so let go ahead and add an address back into the Profile table to be sure everything is fineand yes it worksSo 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.

. More details

Leave a Reply