05-03-2018

Laravel Optional helper

Laravel  Optional helper

Optional helper (availiable from Laravel 5.5) provides a smart and clean way to make if condition.

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.

 

optional(\App\User::find(1)->profile)->isAdmin

or

if(oprional($post)->hasReplies()) {
   .....
}