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…


This content originally appeared on CodeSource.io and was authored by Anik Kanti Sikder Mithu

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.


This content originally appeared on CodeSource.io and was authored by Anik Kanti Sikder Mithu


Print Share Comment Cite Upload Translate Updates
APA

Anik Kanti Sikder Mithu | Sciencx (2022-02-11T08:58:26+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 ,<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 ]. 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 . 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: ]
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/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.