how to use groupBy() method in Laravel

The groupBy() method in Laravel returns the query results by grouping the data of a database table. Suppose we have multiple data of the same…

The groupBy() method in Laravel returns the query results by grouping the data of a database table. Suppose we have multiple data of the same type or name in our database. We can get those specific types of data without any duplicate value by using the groupBy() method. In this tutorial, you are going to learn how to use the groupBy() method in Laravel with examples.

Let’s assume, we have a database table named salaries where we can store the salary of teachers department-wise. There can be multiple departments in the database like this-

 groupBy() method in Laravel

Now, if we want to know, how many departments are available in the salaries table, we can use the groupBy() method to find out. It takes column name as an argument. The example is given below-

Controller:

public function view(){

    $departments = Salary::select('department')
                           ->groupBy('department')
                           ->get();
    return view('view_department', compact('departments'));        
}

From the Salary model, we can select the column name, in our case that is the department, and perform the groupBy() operation to get all the department’s names without any duplication of the data. After retrieving the department data we can simply pass the data through the compact() function to view in the HTML file.

HTML view:

<div class="container">
  <h1>Available Departments</h1><hr>

  @foreach ($departments as $data)
      <p><b> {{$data->department}} </b></p>
  @endforeach
</div>

We can perform a foreach loop with the retrieved data to show the department names.

OUTPUT:

 groupBy() method in Laravel

Here, you can see all the department’s names, and they are not repeated. Though we have multiple duplicate entities in our database with multiple teachers from the same department. So, that’s how you can use the groupBy() method in Laravel. Thank you.


Print Share Comment Cite Upload Translate
APA
Anik Kanti Sikder Mithu | Sciencx (2024-03-28T11:31:04+00:00) » how to use groupBy() method in Laravel. Retrieved from https://www.scien.cx/2022/02/11/how-to-use-groupby-method-in-laravel/.
MLA
" » how to use groupBy() method in Laravel." Anik Kanti Sikder Mithu | Sciencx - Friday February 11, 2022, https://www.scien.cx/2022/02/11/how-to-use-groupby-method-in-laravel/
HARVARD
Anik Kanti Sikder Mithu | Sciencx Friday February 11, 2022 » how to use groupBy() method in Laravel., viewed 2024-03-28T11:31:04+00:00,<https://www.scien.cx/2022/02/11/how-to-use-groupby-method-in-laravel/>
VANCOUVER
Anik Kanti Sikder Mithu | Sciencx - » how to use groupBy() method in Laravel. [Internet]. [Accessed 2024-03-28T11:31:04+00:00]. Available from: https://www.scien.cx/2022/02/11/how-to-use-groupby-method-in-laravel/
CHICAGO
" » how to use groupBy() method in Laravel." Anik Kanti Sikder Mithu | Sciencx - Accessed 2024-03-28T11:31:04+00:00. https://www.scien.cx/2022/02/11/how-to-use-groupby-method-in-laravel/
IEEE
" » how to use groupBy() method in Laravel." Anik Kanti Sikder Mithu | Sciencx [Online]. Available: https://www.scien.cx/2022/02/11/how-to-use-groupby-method-in-laravel/. [Accessed: 2024-03-28T11:31:04+00:00]
rf:citation
» how to use groupBy() method in Laravel | Anik Kanti Sikder Mithu | Sciencx | https://www.scien.cx/2022/02/11/how-to-use-groupby-method-in-laravel/ | 2024-03-28T11:31:04+00:00
https://github.com/addpipe/simple-recorderjs-demo