Check if variable exist in laravel's blade directive

前端 未结 13 1430
清歌不尽
清歌不尽 2021-02-03 19:23

I\'m trying to create blade directive which echo variable (if variable defined) or echo \"no data\" if variable undefined.

This is my code in AppServiceProvider.ph

相关标签:
13条回答
  • 2021-02-03 19:38

    For Laravel 5.7 onwards use.

    {{ $checkvariable ?? 'not-exist' }}

    0 讨论(0)
  • 2021-02-03 19:43

    For Laravel version >=5.7

    {{ $value ?? '' }}
    

    For Laravel version <5.7

    {{ $value or '' }}
    
    0 讨论(0)
  • 2021-02-03 19:43

    The @empty directive might be useful:

    @empty($var)
       $var is unset or false-y
    @endempty
    
    0 讨论(0)
  • 2021-02-03 19:44

    For the last version of Larvael make the variable optional in the blade template. Replace $myvar with {{ $myvar }} with {{ $myvar?? '' }}

    0 讨论(0)
  • 2021-02-03 19:45

    Blade has a directive to check if a variable is set:

    @isset($var)
    
    @endisset
    
    0 讨论(0)
  • 2021-02-03 19:48

    If you trying check a bool variable you can use @unless

    <input type="text" class="@unless ($variable) d-none @endunless" >

    0 讨论(0)
提交回复
热议问题