Ad

{{ $data Or 'Default' }} Is Not Working In Laravel Blade

- 1 answer

I'm trying to use convenient shortcut in blade file, which shows error on the screen.

@extends('layout/master')
@section('content2')
<h1>
    {{date('d m y')}}   <br>
    @{{date('d m y')}}  <br>
    {{ isset($data) ? $data : 'Not' }}
    {{ $data or 'Default' }}
</h1>
@stop
Ad

Answer

What you want to use in that case is null coalescing operator, which was introduced in PHP7.

The expression (expr1) ?? (expr2) evaluates to expr2 if expr1 is NULL, and expr1 otherwise. In particular, this operator does not emit a notice if the left-hand side value does not exist, just like isset(). This is especially useful on array keys.

Therefore you can use it like this:

{{ $data ?? 'Default' }}
Ad
source: stackoverflow.com
Ad