Echoing data with default value in Laravel blade - BOOKC -->

Echoing data with default value in Laravel blade

Saturday, March 25, 2017


When echoing data in laravel blade template using double or triple curly braces, you don't need to worry if the value is empty, it will not displayed as an error, but sometimes you want to put a default value if the data is empty.

Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. Basically, you want to do this:
{{{ isset($name) ? $name : 'Default' }}}

However, instead of writing a ternary statement, blade allows you to use the following convenient short-cut:
{{{ $name or 'Default' }}}

You don't need to do conditional 'if' statement or ternary statement to put a default value when the variable is empty, simply use 'or' and specify the default string value to echo inside curly braces.