0
0
Laravelframework~30 mins

Where clauses in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Filtering Products with Laravel Where Clauses
📖 Scenario: You are building a simple product listing page for an online store. You want to show only products that meet certain conditions, like being in stock or having a price below a certain amount.
🎯 Goal: Build a Laravel query using where clauses to filter products by stock availability and price.
📋 What You'll Learn
Create a products array with product details
Add a variable maxPrice to set the price limit
Use Laravel's where clauses to filter products by in_stock and price
Return the filtered products collection
💡 Why This Matters
🌍 Real World
Filtering data like products or users based on conditions is common in web apps to show relevant information.
💼 Career
Understanding Laravel's where clauses helps you build efficient database queries and data filtering in real projects.
Progress0 / 4 steps
1
Create the products array
Create a variable called products as a Laravel collection containing these exact products: ['name' => 'Laptop', 'price' => 1200, 'in_stock' => true], ['name' => 'Mouse', 'price' => 25, 'in_stock' => true], ['name' => 'Keyboard', 'price' => 75, 'in_stock' => false], and ['name' => 'Monitor', 'price' => 300, 'in_stock' => true].
Laravel
Need a hint?

Use collect([]) to create a Laravel collection with the given product arrays inside.

2
Set the maximum price filter
Create a variable called maxPrice and set it to 100 to filter products cheaper than this price.
Laravel
Need a hint?

Just create a simple variable $maxPrice and assign the number 100.

3
Filter products using where clauses
Use Laravel collection where clauses on $products to filter only products where in_stock is true and price is less than $maxPrice. Save the result in a variable called filteredProducts.
Laravel
Need a hint?

Chain two where calls on $products. First filter in_stock equals true, then filter price less than $maxPrice.

4
Return the filtered products
Return the $filteredProducts collection so it can be used to display the filtered product list.
Laravel
Need a hint?

Use return $filteredProducts; to send back the filtered collection.