Laravel 8 Group By Query Example

SELECT column_names
FROM table_name
WHERE condition
GROUP BY column_names
ORDER BY column_names;
SELECT COUNT(user_id), country
FROM users
GROUP BY country;
use App\User;
use DB;
public function index()
{
$users = User::select("*", DB::raw("count(*) as user_id"))
->groupBy('country')
->get();
}
$users = DB::table('users')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();
DB::table('users')
->select(DB::raw('DATE(created_at) as date'), DB::raw('count(*) as views'))
->groupBy('date')
->get();
$users = User::select("*", DB::raw("count(*) as user_count"))
->groupBy(DB::raw("year(created_at)"))
->get();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store