Laravel Blade - 检查数据数组是否有特定的键

Laravel Blade - check if data array has specific key

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

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

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

您可以使用 @isset:

@isset($data['currentOffset'])
    {{-- currentOffset exists --}}
@endisset

使用以下内容:

@if (array_key_exists('currentOffset', $data))
    <p>Current Offset: {{ $data['currentOffset'] }} </p>
@else
    <p>The key `currentOffset` is not in the data array</p>
@endif

我想你需要这样的东西:

 @if ( isset($data[$currentOffset]) )
 ...

三元

 @php ($currentOffset = isset($data['currentOffset']) ? $data['currentOffset'] : '')