Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Blade - check if data array has specific key

I need to check if the data array has a specific key, I tried it like this:

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

But I always get <p>The keycurrentOffsetis not in the data array</p>.

like image 690
Black Avatar asked Jan 04 '18 17:01

Black


2 Answers

You can use @isset:

@isset($data['currentOffset'])
    {{-- currentOffset exists --}}
@endisset
like image 65
Alexey Mezenin Avatar answered Sep 22 '22 15:09

Alexey Mezenin


Use following:

@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
like image 42
Sahil Purav Avatar answered Sep 23 '22 15:09

Sahil Purav