In this post we will discuss laravel querybuilder tutorial for groupBy and orderBY query. you learn laravel step by step query builder tutorial with video.
How to use groupby and orderby in laravel query builder.
- orderBy()
- groupBy()
- groupBy with orderby
Laravel orderBy() query
Laravel orderby is use for ordering database table column values it set table column to asc and desc order.In orderBy() if we pass only column name than it get data in ascending order of that column.
If we want to set a column name in custom order than pass first argument column name and second is order.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class EmployeeController extends Controller{ public function index(){ $employees = DB::table('employees')->orderBy('name')->get(); //also use $employees = DB::table('employees')->orderBy('name','desc')->get(); dd($employees); } }
Laravel groupBy() method
Laravel query builder groupBy() method is use for get unique data of column which pass in groupBy method. In groupBy we pass single or multiple columns both. groupBy method is use when we perform aggregate operations(count, sum, avg)
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class EmployeeController extends Controller{ public function index(){ $employee = DB::table('employees')->groupBy('sallery')->get(); dd($employees); } }
Laravel groupBy with orderby query
we also use orderby method with groupby. In this case groupby select unique rows and order by set them order. like if we want to select unqique salleries with descending order.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class EmployeeController extends Controller{ public function index(){ $employee = DB::table('employees')->groupBy('sallery')->oderBy('sallery','desc')->get(); dd($employees); } }
You read this tutorial on advanced web tuts. watch all query builder playlist click on sidebar playlist link
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- laravel query builder introduction tutorial
- part 1 laravel query insert data
- part 2 laravel all type of where conditions in query builder
- part 3 laravel whereDate, whereYear and whereTime qyery method
- part 4 compare two db columns in laravel query builder
- part 6 laravel pluck method get single column value
- part 7 laravel select and addselect query in query builder
- part 5 laravel first and find queries
- part 8 laravel edit and update method query explaination
- Part 9 laravel query for update or insert data by one method
- part 10 laravel delete vs truncate vs drop method
- part 11 gropupby and orderby query tutorial
- Part 12 laravel union of two queries
- part 13 laravel all aggregate functions with example
- part 14 laravel query builder chunk method
- part 15 laravel exists() and doesntExist() method
- part 16 laravel paginate vs simplePaginate method
- part 17 laravel all join methods