Laravel Blade - 检查数据阵列是否具有特定密钥

Bla*_*ack 6 laravel laravel-blade

我需要检查数据数组是否有特定的密钥,我试过这样:

@if ( ! empty($data['currentOffset']) )
    <p>Current Offset: {{ $currentOffset }} </p>
@else
    <p>The key `currentOffset` is not in the data array</p>
@endif
Run Code Online (Sandbox Code Playgroud)

但我总是得到<p>The keycurrentOffset is not in the data array</p>.

Ale*_*nin 12

你可以使用@isset:

@isset($data['currentOffset'])
    {{-- currentOffset exists --}}
@endisset
Run Code Online (Sandbox Code Playgroud)

  • @黑不。如果你需要 `else`,你可以只使用 `@if (isset($data['currentOffset']))` 或三元运算符 `{{ isset($data['currentOffset']) ?'存在' : '不存在' }}` (2认同)

You*_*neL 5

我认为你需要这样的东西:

 @if ( isset($data[$currentOffset]) )
 ...
Run Code Online (Sandbox Code Playgroud)