MySQL having is used to filter records based on the condition passes but it applies on last of query, where we can access aggregated function of select query. so to create the having query we used having
method in laravel. laravel having
method accepts multiple parameter same as laravel where.
In this article I will show you to use having in multiple ways, laravel also provide to pass raw query to having so we can pass multiple parameter to having.
Here is the syntax of having
Article::selectRaw("concat('first_name',' ','last_name') as full_name")->having('full_name','test test');
//or
Article::select("first_name")
->having(\DB::raw("concat('first_name',' ','last_name')"),'test test');
//or
Article::having('column_name','operator','value');
//or
Article::having('column_name','value');
SQL of above query will be
select concat('first_name',' ','last_name') as full_name from `table` having full_name = 'test test'
//OR
select `first_name` `table` having concat('first_name',' ','last_name') = 'test test'
//OR
select * from `table` having `column_name` = 'value'
So let’s understand laravel having with example
Example 1 – Laravel having method with single column and value
In this example I will show to use MySQL having method in laravel eloquent. having can accept multiple column and we will pass single column and value 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("*")->having('status',1);
\\or
DB::table('articles')->having("status",1);
}
}
Output will be :
select `*` from `articles` having `status` = 1
Example 2 – Laravel having with operator like !=, like, etc.
In this example I will show to use laravel eloquent having method by passing operator to match the condition. in this example i will use opertor like
in our 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::selectRaw(\DB::raw("concat('first_name',' ','last_name') as full_name"))
->having("full_name","like","test");
}
}
Output will be :
select concat('first_name',' ','last_name') as full_name from `articles` having full_name like 'test'
Here we passed multiple parameter to groupBy
method which is acceptable by laravel eloquent and query builder
Example 3 – Laravel having with raw query
Sometimes we need to pass raw query like count or some aggregate function in having 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("status")->having(\DB::raw('count(*) > 2');
//OR
$Article = Article::select("status")->having(\DB::raw('count(*)',">",2);
}
}
Output will be :
select `status` from `articles` having count(*) >2
I hope this makes clear to you.