How to echo a default value if value not set blade

LaravelLaravel 4Laravel 5BladeTemplating

Laravel Problem Overview


I would like to know what would be the best way to display a default value if the given value is not set. I have the following in a blade file (I can not guaranty that the key is set, it depends on a multitude of factors).

{{ $foo['bar'] }}

I would know if the following is the best way to go about it,

{{ (isset($foo['bar']) ? $foo['bar'] : 'baz' }}

or is there a better way to do this?

Thanks :)

Laravel Solutions


Solution 1 - Laravel

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'; 

Solution 2 - Laravel

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" }}

Solution 3 - Laravel

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

Solution 4 - Laravel

I recommend setting the default value in your controller instead of making a work-around in your view.

This is the best way because it keeps logic out of your view, and keeps the view's markup clean.

For example in your controller, before passing data to the view:

if(!isset($foo['bar'])){
     $foo['bar'] = 'baz';
}

Solution 5 - Laravel

Usually I use the null coalescing operator (introduced in PHP 7.0):

{{ $foo['bar'] ?? 'baz' }}

However, if 'baz' is actually a long expression (such as several lines of HTML, possibly with some PHP variables), then I use Blade‘s @isset (@else is optional but works):

@isset($foo['bar'])
    {{ $foo['bar'] }}
@else
    {{ $b }}
    <br>
    {{ $a }}
    <br>
    {{ $z }}
@endisset

(Taking a little liberty with the example, obviously.)

There’s also @empty($expr) which has similar syntax but works the same as the PHP function of the same name. This may be helpful: https://stackoverflow.com/q/1219542/6083675

Solution 6 - Laravel

While Chris B's answer is perfectly valid; I felt perhaps this is a question that can have an alternative answer. Some would prefer not to make their controllers "fat" and in this case at least, the use of a Presenter could be the answer you seek for allowing a great deal of flexibility in your application views.

Take a look at the following project/package on Github. The readme is pretty robust with a number of examples to get you going.

It will allow you to do just what you asked and simply call

{{ $foo['bar'] }}

in your view.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpaquettgView Question on Stackoverflow
Solution 1 - LaravelJazerixView Answer on Stackoverflow
Solution 2 - LaraveljoemallerView Answer on Stackoverflow
Solution 3 - LaravelEdmund SulzanokView Answer on Stackoverflow
Solution 4 - LaravelChris BierView Answer on Stackoverflow
Solution 5 - LaravelLaurelView Answer on Stackoverflow
Solution 6 - LaraveltwmbxView Answer on Stackoverflow