How to Use SQL Having Clause With Example In Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings.

How to Use SQL Having Clause in Laravel

How we use SQL HAVING Clause
Syntax

SELECT column_name list
FROM table_name
WHERE condition
GROUP BY column_name list
HAVING condition
ORDER BY column_name list;


To learn more about SQL, please visit our BajarangiSoft site.

How can you use having clause statement in laravel
having methods may be used to group the query results. The having method's signature is similar to that of the where method:


Example(1)
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;//include this

class UserController extends Controller
{
    public function index()
    {
        $users = DB::table('users')
            ->groupBy('first_name', 'status')
            ->having('account_id', '>', 100)
            ->get();
        return view('user.index', ['users' => $users]);
    }
}
?>

the above example group the first_name and status where having clause get the account_id greater than 100 and return the data from controller to index.blade.php file.

Related Post