Reputation: 13
@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
Reputation: 163838
You can use @forelse
as 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
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