Laravel cool helpers, Optional()

optional()
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:

return optional($user->address)->street;

{!! old('name', optional($user)->name) !!}
The optional function also accepts a Closure as its second argument. The Closure will be invoked if the value provided as the first argument is not null:

return optional(User::find($id), function ($user) {
return new DummyUser;
});
CategoriesUncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *