0
0
Laravelframework~5 mins

Control structures (@if, @foreach, @for) in Laravel - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the @if directive do in Laravel Blade templates?
The @if directive checks a condition and shows the enclosed HTML only if the condition is true. It's like asking a question: if this is true, then do something.
Click to reveal answer
beginner
How do you loop over a list of items in Blade using @foreach?
You use @foreach to repeat a block of HTML for each item in a list. It’s like saying: for each item in this list, show me this HTML with that item’s data.
Click to reveal answer
intermediate
What is the difference between @foreach and @for in Blade?
@foreach loops over each element in a collection or array, while @for loops a set number of times using a counter. Use @foreach for lists, and @for when you know how many times to repeat.
Click to reveal answer
intermediate
How do you write an @if-@elseif-@else structure in Blade?
You start with @if(condition), then add @elseif(another condition) for extra checks, and finish with @else for the default case. This lets you choose between multiple options.
Click to reveal answer
advanced
Can you use @continue and @break inside @foreach loops in Blade? What do they do?
Yes! @continue skips the current loop iteration and moves to the next item. @break stops the loop completely. They help control the loop flow like traffic signals.
Click to reveal answer
What will @if($user->isAdmin()) do in a Blade template?
AAlways show content
BLoop through all users
CShow content only if the user is an admin
DHide content if user is admin
Which directive is best to loop over an array of posts in Blade?
A@if
B@foreach
C@for
D@while
What does @break do inside a @foreach loop?
ASkips the current item
BDoes nothing
CRestarts the loop
DStops the loop completely
How do you write a loop that runs exactly 5 times in Blade?
A@for($i = 0; $i < 5; $i++)
B@if($i < 5)
C@foreach($items as $item)
D@while($i < 5)
Which directive lets you check multiple conditions in Blade?
A@if, @elseif, @else
B@foreach, @for
C@switch, @case
D@while, @break
Explain how to use @if, @elseif, and @else in a Blade template to handle different conditions.
Think of it like asking questions one after another.
You got /4 concepts.
    Describe the difference between @foreach and @for loops in Blade and when to use each.
    One is for lists, the other for counting.
    You got /4 concepts.