In this tutorial you will learn about the Laravel 8 Group By Example groupBy() Value in Laravel and its application with practical example.
In this Laravel 8 Group By Example tutorial I will show you how to use groupby function in laravel query. In this tutorial you will learn to use groupby function in laravel query. In this article I will share various example to use groupby function in laravel queries. In this example I will share groupby syntax and example in laravel queries.
Laravel groupBy()
The laravel groupBy() function is used for grouping the query results in Laravel. It allow us to group the data by specified criteria.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; use DB; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $users = User::select("*", DB::raw("count(*) as user_count")) ->groupBy('status') ->get(); dd($users); } } |