How to echo a default value if value not set blade How to echo a default value if value not set blade laravel laravel

How to echo a default value if value not set blade


Use php's null coalesce operator:

{{ $variable ?? "Default Message" }}

Removed as of Laravel 5.7

With Laravel 4.1-5.6 you could simply do it like this:

{{ $variable or "Default Message" }}

It's the same as:

echo isset($variable) ? $variable : 'Default Message'; 


PHP 5.3's ternary shortcut syntax works in Blade templates:

{{ $foo->bar ?: 'baz' }}

It won't work with undefined top-level variables, but it's great for handling missing values in arrays and objects.

Also, for instance, if we want to show up the date an organization was created, which might not exist (for example, if we create it manually from the DB and don't link to it any records for that field), then do something like

{{ $organization->created_at ? $organization->created_at->format('d/m/Y H:i') : "NULL" }}


Since Laravel 5.7 {{$Variable or "Default Message"}} throws $Variable is not defined. This {{$Variable ?? "Default Message"}} works though.