How to echo a default value if value not set blade
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Handling Null Values and Default Display in Blade Templates - A Comprehensive Guide
Body:
Displaying default values or substituting them with alternative values when the given key is not set is a common requirement in web development. In this blog post, we will explore different approaches to handling null values and default display in Blade templates using Laravel.
Firstly, let's analyze the code snippet provided:
```php
{{ $foo['bar'] }}
```
This is a simple way of accessing an array element named 'bar' using the $foo variable and displaying it within a Blade template. The main issue arises when the key 'bar' may or may not be set, making it essential to handle this scenario.
One approach to handle null values in the given code snippet is:
```php
{{ (isset($foo['bar']) ? $foo['bar'] : 'baz' }}
```
This code uses the PHP ternary operator to check if the key exists using isset(). If it is set, the value will be displayed; otherwise, the default value ('baz') will show up. Although this method works, there are better ways to achieve this result.
A more elegant and maintainable solution could involve using Laravel's built-in collection helper methods:
```php
{{ $foo->get('bar', 'baz') }}
```
Here, we use the get() method on the $foo variable. The first parameter is the key you want to retrieve from the array, while the second parameter specifies the default value to return if the given key does not exist. This approach ensures that your code remains well-organized and readable.
Another option is to utilize null coalescing operator:
```php
{{ $foo['bar'] ?? 'baz' }}
```
This syntax checks if the value of the array key 'bar' is null and assigns either that value or the alternative string ('baz') depending on the condition. Although this approach requires fewer lines of code, using isset() remains a better practice in most situations as it can be more explicit about its intention to check for existence rather than simply checking for null values.
Lastly, you may want to use an if statement:
```php
{{ $foo['bar'] ?: 'baz' }}
```
This line uses the PHP short-circuit operator (?:) to evaluate the given key's value and display either that or the default value. Using this method can lead to less readable code, so it is advised to use the previous methods for better maintainability.
To sum up, when handling null values and default display in Blade templates using Laravel, you have a range of options at your disposal. The best approach depends on individual preferences and requirements. Remember to always keep your code organized, readable, and scalable by choosing the appropriate method for each specific situation.
As a final note, always ensure that the default value used in any of these approaches is relevant to the context and clearly understood by other team members or third-party developers. This will help prevent confusion and maintain consistency within your codebase.