Performance: Ordering and grouping
Ordering and grouping affect how quickly the database returns sorted or aggregated data, impacting page load speed and responsiveness.
DB::table('orders')->groupBy('status')->orderBy('created_at')->get();
DB::table('orders')->get()->sortBy('created_at')->groupBy('status');
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Ordering and grouping in PHP after fetch | N/A | N/A | Blocks rendering until data ready | [X] Bad |
| Ordering and grouping in database query | N/A | N/A | Faster data ready for rendering | [OK] Good |
orderBy method do in Laravel's Eloquent?orderByorderBy method is used to sort query results by a column in ascending or descending order.created_at date descending in Laravel?orderBy with column and direction as parameters to sort results.orderBy is correct, desc is lowercase string, and get() fetches results. Other options use invalid methods.$results = DB::table('orders')
->select('status', DB::raw('count(*) as total'))
->groupBy('status')
->orderBy('total', 'desc')
->get();$results contain?status and counts how many orders each status has, naming count as total.total descending, so statuses with more orders appear first.$users = User::groupBy('role')->orderBy('name')->get();having('total', '>', 5) to filter categories with more than 5 sales. where cannot filter aggregated results.orderBy('total', 'desc') after grouping and filtering.Pass the test and this pattern moves unknown