Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel having in eloquent with example

How to use laravel having with example ?

Aman Jain, January 22, 2022January 22, 2022

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.

Related

Php Laravel havinglaravelmysql

Post navigation

Previous post
Next post

Related Posts

Php How to Change Date Format in Blade View File in Laravel

How to Change Date Format in Blade View File in Laravel ?

September 8, 2022March 16, 2024

Laravel default shows the date in the way its stored in database bu sometimes we want to change date format in blade view file in laravel according to our requirements. It can be multiple possibilities and requirement from client or product manager to change the date format so in this…

Read More
Php Laravel 11 CRUD Example Tutorial with Search, Image and Pagination

Laravel 11 CRUD Example Tutorial with Search, Image and Pagination

July 5, 2024July 5, 2024

In this article, we’ll delve into Laravel and explore the implementation of CRUD operations alongside essential features like Search, Image uploading, and Pagination. CRUD, an acronym for Create, Read, Update, and Delete, forms the backbone of database management in web applications across all programming paradigms. This tutorial covers these fundamental…

Read More
Php laravel belongsTo

How to Use Laravel belongsTo Relationship in with Example ?

February 27, 2022November 10, 2023

Laravel BelongsTo relationship is used to create the relation between two tables. belongsTo means create the relation one to one in inverse direction or its opposite of hasOne. For example if a user has a profile and we wanted to get profile with the user details then we can use…

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

  • August 2025
  • July 2025
  • 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 Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version