Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to order users by their name in ascending order.
Laravel
$users = User::orderBy('[1]')->get();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column that does not exist.
Forgetting to put the column name in quotes.
✗ Incorrect
Ordering by 'name' sorts users alphabetically by their name.
2fill in blank
mediumComplete the code to group orders by their status.
Laravel
$orders = Order::select('status', DB::raw('count(*) as total')) ->groupBy('[1]') ->get();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by a column not selected.
Using a wrong column name.
✗ Incorrect
Grouping by 'status' groups orders by their status value.
3fill in blank
hardFix the error in ordering products by price descending.
Laravel
$products = Product::orderBy('price', '[1]')->get();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'descending' instead of 'desc'.
Using 'asc' for descending order.
✗ Incorrect
Use 'desc' to order by descending price in Laravel.
4fill in blank
hardFill both blanks to order posts by created date descending and group by user_id.
Laravel
$posts = Post::orderBy('[1]', '[2]') ->groupBy('user_id') ->get();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping order direction.
Using wrong column names.
✗ Incorrect
Order posts by 'created_at' descending, then group by 'user_id'.
5fill in blank
hardFill all three blanks to select category, count products, group by category, and order by count descending.
Laravel
$result = Product::select('[1]', DB::raw('count(*) as [2]')) ->groupBy('[3]') ->orderBy('[2]', 'desc') ->get();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different columns for select and groupBy.
Forgetting to alias the count.
Ordering by a column not selected.
✗ Incorrect
Select 'category', count products as 'total', group by 'category' and order by 'total' descending.