Hi dev.
In this article we will learn example about Forelse loop in Laravel Blade. Let's start …
If you have ever done any coding you are most likely well familiar with the foreach loops. Without any doubt, a foreach loop is one of the best ways to iterate over the elements of the collection.
However, in case you have an empty collection, you would need an additional if statement so that you could point a valid message to your users.
Luckily Laravel provides awesome Blade Templates that you could use to make your life easier!
In this post, I will show you how to use forelse in Laravel!
Before you get started you would need to have Laravel already installed.
What I would usually do in order to check if a collection is empty in my Blade view is to use an if-else
statement like this:
@if($posts->isNotEmpty())
@foreach ($posts as $post)
<p>This is the post title {{ $post->title}}</p>
@endforeach
@else
<p>No posts found</p>
@endif
In the above example, we first wrap up our foreach
loop in an if statement, and by using the isNotEmpty()
we check if
the collection is empty, and in case that it is empty we then print the No posts found
message.
This works pretty well, but Laravel has a more elegant way of doing things!
Forelse example
Rather than having to nest our foreach
loop inside of an if
statement, what we could do instead is use the forelse
blade template:
@forelse ($posts as $post)
<p>This is the post title {{ $post->title}}</p>
@empty
<p>No posts found</p>
@endforelse
As you can see we would get the same result but with less code and it is much easier to read!
You now know how to use forelse
in Blade views and have much more cleaner and easier to read code!
For more great Blade templates, I would recommend checking out the official documentation here.
Hope it will will helps you!
Categories : Laravel
order tricor 200mg pill fenofibrate 200mg usa fenofibrate 200mg price
generic zaditor 1mg ziprasidone 80mg generic order imipramine without prescription
buy cheap generic minoxytop tadalafil 40mg price buy generic ed pills for sale
buy aspirin 75 mg without prescription levoflox 250mg over the counter order imiquimod sale
dipyridamole us pravachol 20mg usa order pravastatin pills
melatonin 3 mg us meloset 3mg price danocrine 100mg usa
duphaston 10mg generic sitagliptin 100mg without prescription generic jardiance 25mg
florinef online buy cheap aciphex 20mg loperamide 2 mg without prescription
monograph 600mg cheap pletal 100 mg sale order pletal 100mg for sale
buy prasugrel 10mg pill buy dramamine medication detrol 2mg generic
buy ferrous 100mg for sale buy ferrous 100 mg online purchase sotalol for sale
buy cheap generic pyridostigmine piroxicam buy online maxalt 10mg price
vasotec for sale vasotec price buy duphalac bottless
purchase betahistine pill how to buy haloperidol buy probenecid paypal
order prilosec 10mg generic metoprolol without prescription buy metoprolol paypal
premarin without prescription sildenafil 50mg usa viagra 50mg tablet
buy micardis 20mg for sale hydroxychloroquine cheap order molnupiravir generic
order cialis 5mg tadalafil 5mg price cheap sildenafil online
cenforce usa naproxen 250mg price order chloroquine 250mg online
order provigil online order deltasone 5mg online prednisone online
cefdinir online buy order glycomet online cheap prevacid us
purchase absorica for sale buy isotretinoin 20mg pill zithromax 250mg sale
buy lipitor 10mg online buy generic norvasc norvasc medication
buy azithromycin 250mg buy azipro online buy neurontin 800mg online
online casino real money usa order furosemide online order furosemide generic
order pantoprazole 40mg pills buy phenazopyridine 200mg phenazopyridine over the counter
online blackjack free blackjack online us albuterol inhaler