As a tradition of posting little things here on the blog, one more useful detail. What do you do when you need to show a loop in Blade with foreach, but the list might be empty? You probably write if-else statement around it, right?
There’s a “magic” loop structure called forelse. Here’s how it works.
Instead of:
@if ($users->count()) @foreach ($users as $user)This is user {{ $user->id }}
@endforeach @elseNo users found.
@endif
You can use this:
@forelse ($users as $user)This is user {{ $user->id }}
@emptyNo users found.
@endforelse
Nice, isn’t it?
Hello, how can we user several forelse’s and one empty?
Thanks in advance.
My mind is officially blown….plus that meme also helped blow it
Is @empty optional?
Yes