Building a Simple Product List with Laravel Blade Control Structures
📖 Scenario: You are creating a simple product list page for a small online store using Laravel Blade templates. You want to show the products with their prices, highlight expensive products, and display a message if no products are available.
🎯 Goal: Build a Blade template that uses @foreach to list products, @if to highlight expensive products, and @for to show a limited number of featured products.
📋 What You'll Learn
Create a $products array with exact product names and prices
Create a variable $expensiveThreshold to define the price limit for expensive products
Use @foreach to loop through $products and display each product's name and price
Use @if inside the loop to highlight products with price greater than $expensiveThreshold
Use @for to display the first 3 featured products from the $products array
💡 Why This Matters
🌍 Real World
Online stores and dashboards often need to display lists of items with conditional formatting and limited featured sections.
💼 Career
Understanding Blade control structures is essential for Laravel developers to build dynamic and maintainable web pages.
Progress0 / 4 steps
1
Create the products array
Create a PHP array called $products with these exact entries: ['Laptop' => 1200, 'Smartphone' => 800, 'Tablet' => 450, 'Headphones' => 150, 'Charger' => 50].
Laravel
Hint
Use square brackets [] to create the array with keys as product names and values as prices.
2
Add the expensive price threshold
Create a variable called $expensiveThreshold and set it to 500.
Laravel
Hint
Just assign the number 500 to the variable $expensiveThreshold.
3
Use @foreach and @if to list and highlight products
Write a Blade template snippet that uses @foreach ($products as $product => $price) to loop through the products. Inside the loop, use @if ($price > $expensiveThreshold) to add a <strong> tag around the product name for expensive products. Display each product name and price inside a <li> element.
Laravel
Hint
Use @foreach to loop, @if to check price, and Blade curly braces {{ }} to show variables.
4
Use @for to display first 3 featured products
Add a Blade @for loop that runs from $i = 0 to less than 3. Inside the loop, display the product name and price from array_keys($products)[$i] and array_values($products)[$i] inside a <div> with class featured.
Laravel
Hint
Use @for with $i from 0 to 2, and use array_keys($products)[$i] and array_values($products)[$i] to get product name and price.
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.