Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel group by with example

How to use laravel group by with example ?

Aman Jain, January 22, 2022January 22, 2022

MySQL group by is used to group same column value multiple rows into one row so to create the group by query we used groupBy method. laravel groupBy method accepts single and multiple parameter as column name.

In this article i will show you to use groupBy in multiple ways, laravel also provide to pass raw query to group so we can pass multiple parameter to group by.

Here is the syntax of groupBy

Article::select("status")->groupBy('status');  // uses mysql group by method and more performant

//or

Article::select("status")->groupBy(DB::raw('status as is_active'))->first();  // uses mysql group by method but we can pass raw sql query

SQL of above query will be

select status from `table` group by status

//OR

select statusfrom `table` group by status as is_active

So let’s understand laravel group by rows with example

Example 1 – Laravel groupBy method with single column

In this example I will show to use MySQL group by method in laravel eloquent. groupBy can accept multiple column and we will pass single column in this example

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select("*")->groupBy('status');
      \\or
      DB::table('articles')->groupBy("status");

   }
}

Output will be :

select `statsu` from `articles` group by `status`

Example 2 – Laravel group by with multiple column

In this example I will show to use laravel eloquent group by method with multiple column in group by

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select("select","is_active")->groupBy("select","is_active");
         
    }
}

Output will be :

select `status`,`is_active` from `articles` group by `status`,`is_active`

Here we passed multiple parameter to groupBy method which is acceptable by laravel eloquent and query builder

Example 3 – Laravel group by with raw query

Sometimes we need to pass raw query like count or some aggregate function in group by query so we can use as below

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select("select")->groupBy(\DB::raw('status as newstatus'));
         
    }
}

Output will be :

select `status` from `articles` group by status as newstatus

I hope this makes clear to you.

Also read : How to use laravel count rows with example ?

Related

Php Laravel groupbylaravelmysql

Post navigation

Previous post
Next post

Related Posts

Php Laravel Custom Facade with example

How to create a custom facade with example in Laravel 8 ?

December 22, 2021January 10, 2022

In Laravel there several facades to use like DB, URL, Validator ,Request etc. Facades are used to use the class methods statically using the application service container. In this article we will learn to create our own facades with service container. Let’s start with step by step For a better…

Read More
Php Laravel session

How to set and get session in Laravel 8?

November 8, 2021November 17, 2021

Laravel has its own class to manage the session using the file system, redis, array, cookie and database. Session are used to keep the information about the user across the multiple request. As we know PHP is a server side language and all information lost after the response, so keep…

Read More
Php How to Implement JWT Auth in Laravel

How to Implement JWT Auth in Laravel 9 ?

May 31, 2022August 20, 2022

JWT (Json Web Token) is used to create the authentication between two parties client and server that means it creates a secure information transactions between two parties. In the process of php-open-source-saver/jwt-auth token it creates a token claims and generates a token behalf of these claims then it verify on…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version