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
✗ Incorrect
The @if directive checks the condition inside the parentheses. If $user->isAdmin() returns true, the content inside @if will show.
Which directive is best to loop over an array of posts in Blade?
A@if
B@foreach
C@for
D@while
✗ Incorrect
@foreach is designed to loop over each item in an array or collection, perfect for posts.
What does @break do inside a @foreach loop?
ASkips the current item
BDoes nothing
CRestarts the loop
DStops the loop completely
✗ Incorrect
@break immediately stops the loop, so no more items are processed.
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)
✗ Incorrect
@for is used when you want to repeat something a fixed number of times, like 5.
Which directive lets you check multiple conditions in Blade?
A@if, @elseif, @else
B@foreach, @for
C@switch, @case
D@while, @break
✗ Incorrect
The @if, @elseif, and @else directives let you check different conditions and choose what to show.
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.
Practice
(1/5)
1. What does the @if directive do in a Laravel Blade template?
easy
A. It shows content only if a condition is true.
B. It repeats content for each item in a list.
C. It repeats content a fixed number of times.
D. It defines a variable inside the template.
Solution
Step 1: Understand the purpose of @if
The @if directive is used to check a condition and show content only when that condition is true.
Step 2: Compare with other directives
@foreach repeats content for each item, and @for repeats content a set number of times, so they do not match the description.
Final Answer:
It shows content only if a condition is true. -> Option A
Quick Check:
@if = conditional display [OK]
Hint: @if controls conditional display of content [OK]
Common Mistakes:
Confusing @if with @foreach or @for
Thinking @if repeats content
Assuming @if defines variables
2. Which of the following is the correct syntax to start a @foreach loop in Laravel Blade?
easy
A. @foreach $items as $item
B. @foreach $item in $items
C. @foreach($item in $items)
D. @foreach($items as $item)
Solution
Step 1: Recall correct @foreach syntax
The correct syntax uses parentheses and the keyword as: @foreach($items as $item).
Step 2: Identify incorrect options
Options A and D miss parentheses, and C uses incorrect syntax in instead of as.
Final Answer:
@foreach($items as $item) -> Option D
Quick Check:
@foreach syntax = parentheses + 'as' [OK]
Hint: Use parentheses and 'as' keyword in @foreach [OK]
D. @if($products)
@foreach($products as $product)
{{ $product->name }}
@endforeach
@endif
Solution
Step 1: Check condition to display list
@if(count($products) > 0)
@foreach($products as $product)
{{ $product->name }}
@endforeach
@endif uses @if(count($products) > 0) which correctly and explicitly checks if products exist before looping.
Step 2: Analyze other options
B places the check inside the loop, which is redundant since @foreach skips empty arrays. C uses manual @for with array indexing, which is error-prone (e.g., for associative arrays) and less idiomatic than @foreach. D uses @if($products), which works via implicit boolean conversion (empty arrays are falsy) but is less explicit and readable than count > 0.
Final Answer:
@if(count($products) > 0) followed by @foreach -> Option A
Quick Check:
Check count > 0 before @foreach [OK]
Hint: Use count() > 0 to check non-empty arrays before loops [OK]
Common Mistakes:
Using implicit @if($products) instead of explicit count > 0
Placing condition inside loop unnecessarily
Using @for with inner @if redundantly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.