📖 Scenario: You are building a simple Laravel web page that shows a list of fruits and highlights those that are fresh.
🎯 Goal: Create a Blade template that displays a list of fruits from a given array. Use Blade syntax to loop through the fruits and conditionally show 'Fresh!' next to fruits that are fresh.
📋 What You'll Learn
Create a Blade variable called $fruits with exact entries
Create a Blade variable called $freshThreshold with the exact value
Use a @foreach loop with variables $fruit and $status to iterate over $fruits
Use a Blade @if directive to check if $status equals 'fresh' and display 'Fresh!' text
💡 Why This Matters
🌍 Real World
Blade templates are used in Laravel to create dynamic HTML pages that respond to data and user input.
💼 Career
Understanding Blade syntax is essential for Laravel developers to build maintainable and efficient web applications.
Progress0 / 4 steps
1
Create the fruits array
Create a Blade variable called $fruits as an associative array with these exact entries: 'Apple' => 'fresh', 'Banana' => 'ripe', 'Cherry' => 'fresh', 'Date' => 'dry'.
Create a Blade variable called $freshThreshold and set it to the string 'fresh'.
Laravel
Hint
Just assign the string 'fresh' to the variable $freshThreshold.
3
Loop through fruits with Blade @foreach
Use a Blade @foreach loop with variables $fruit and $status to iterate over $fruits. Inside the loop, display the fruit name using {{ $fruit }}.
Laravel
Hint
Use @foreach ($fruits as $fruit => $status) and close with @endforeach.
4
Add conditional freshness label
Inside the @foreach loop, use a Blade @if directive to check if $status equals $freshThreshold. If true, display the text Fresh! next to the fruit name inside the paragraph.
Laravel
Hint
Use @if ($status == $freshThreshold) and close with @endif inside the paragraph.
Practice
(1/5)
1. Which Blade syntax is used to safely display a variable's value in a view?
easy
A. Using PHP echo tags like
B. Using single curly braces like { $variable }
C. Using double curly braces like {{ $variable }}
D. Using square brackets like [ $variable ]
Solution
Step 1: Identify Blade syntax for output
Blade uses double curly braces {{ }} to display variables safely by escaping HTML.
Step 2: Compare options
Only Using double curly braces like {{ $variable }} uses {{ $variable }}, which is the correct Blade syntax.
Final Answer:
Using double curly braces like {{ $variable }} -> Option C
Quick Check:
Safe variable display = {{ $variable }} [OK]
Hint: Remember: {{ }} is for safe variable output in Blade [OK]
Common Mistakes:
Using single braces which is invalid in Blade
Using raw PHP echo instead of Blade syntax
Confusing square brackets with Blade syntax
2. Which of the following is the correct Blade directive to start a conditional statement?
easy
A. @if(condition)
B. #if(condition)
C. if(condition):
D. @@if(condition)
Solution
Step 1: Recall Blade directive syntax
Blade uses directives starting with @ for control structures like conditionals.
Step 2: Identify correct conditional start
The correct syntax to start an if statement is @if(condition).
Final Answer:
@if(condition) -> Option A
Quick Check:
Blade if start = @if(condition) [OK]
Hint: Blade directives always start with @ symbol [OK]
Common Mistakes:
Using # instead of @ for directives
Using raw PHP if syntax inside Blade
Double @ signs which are for escaping
3. What will be the output of this Blade snippet if $name = 'Alice'?
Step 1: Identify correct conditional syntax for object property
Blade @if treats object boolean properties like $user->email_verified as truthy if verified.
Step 2: Evaluate each snippet's logic and syntax
Snippet A with @if($user->email_verified) correctly shows only verified users. Snippet B uses array syntax $user[email_verified] invalid on objects. Snippet C with @if($user->email_verified == true has syntax error (missing closing )). Snippet D uses = true (assignment), always true, shows all users.
Final Answer:
Option A -> Option A
Quick Check:
Object property truthy check [OK]
Hint: Use == for comparison, not = which assigns [OK]
Common Mistakes:
Using = instead of == in conditions
Using array syntax on objects
Forgetting closing parenthesis in @if conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.