0
0
Laravelframework~10 mins

Ordering and grouping in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Acreated_at
Bemail
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column that does not exist.
Forgetting to put the column name in quotes.
2fill in blank
medium

Complete 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'
Astatus
Buser_id
Ccreated_at
Dprice
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by a column not selected.
Using a wrong column name.
3fill in blank
hard

Fix 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'
Aascending
Basc
Cdesc
Ddescending
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'descending' instead of 'desc'.
Using 'asc' for descending order.
4fill in blank
hard

Fill 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'
Acreated_at
Bdesc
Casc
Dupdated_at
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping order direction.
Using wrong column names.
5fill in blank
hard

Fill 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'
Acategory
Btotal
Ccategory_id
Dprice
Attempts:
3 left
💡 Hint
Common Mistakes
Using different columns for select and groupBy.
Forgetting to alias the count.
Ordering by a column not selected.