Zubair Ahmad M Zai
Zubair Ahmad M Zai

Reputation: 13

if the array were empty in laravel . then how to dispaly the message that their are no record found

@foreach($array as $value)
    {{$value}}
@endforeach

$array is my array pass from Laravel controller then i want to display the message record not found if the array were empty. this is my code example.

Upvotes: 1

Views: 76

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163838

You can use @forelseas Shoukat Mirza in his answer and you also could use @if clause:

@if (count($array) > 0)
    // foreach loop here
@endif

This is also helpful when you have multiple conditionals to check.

Upvotes: 0

Shoukat Mirza
Shoukat Mirza

Reputation: 828

Besides 'foreach' loops, we also got 'forelse' loops in laravel blade template, what exactly this 'forelse' loops does? and most importantly should we care about it?

The 'forelse' loops is the better version of 'foreach', so yes, you should care, 'forelse' loops works exactly as 'foreach' except it also check the value is empty or not.

So with 'foreach' normally you check first whether the value is empty or not using 'if' statement, using 'forelse' you don't need to do that, the value is automatically checked.

@forelse ($array as $value)
    {{ $value }}
@empty
    There are no record found.
@endforelse

Upvotes: 4

Related Questions