# How to use groupby having with DB::raw in Laravel Query Builder? (ok)

[Đọc bài viết này đã có dữ liệu test](https://learnphp.gitbook.io/learnphp/laravel-advanced/laravel-orderby-random-using-rand-and-db-raw-example-ok) (tương ứng ví dụ 1)

## How to use groupby having with DB::raw in Laravel Query Builder? (ok)

By Hardik Savani July 6, 2016 Category : Laravel![](https://a.vdo.ai/core/assets/img/cross.svg)PlayUnmuteLoaded: 1.17%Fullscreen[![VDO.AI](https://a.vdo.ai/core/assets/img/logo.svg)](https://vdo.ai/?utm_medium=video\&utm_term=itsolutionstuff.com\&utm_source=vdoai_logo)

We most probably require to use group by having because if you work on small project then it's not need to use generally. But if you work with big project like e-commerce, social or ERP level project then you may require to use having clause.

you can also use groupby having with db raw in laravel 6, laravel 7, laravel 8 and laravel 9 app.

having clause, we can use simply if we have to compare with number or static value like as bellow:

```
->having("total_quantity","<",10)
```

But we need to compare with 10 instead of column name then it can't directly use column name. At that time we should use DB::raw() with column name.

In this example i want to show minimum quantity items, I have two tables one items and another one items\_count both mysql table layout is here:

**items table:**

![](https://www.itsolutionstuff.com/upload/having-items.png)

**items\_count table:**

![](https://www.itsolutionstuff.com/upload/having-items-count.png)

I need to get that product has less quantity to min\_quantity of items table, so we check get that items using bellow query:

**Example**

```
$items = DB::table("items")
            ->select("items.id","items.title"
                ,"items.min_quantity"
                ,DB::raw('SUM(items_count.quantity) as total_quantity'))
            ->join("items_count","items_count.id_item","=","items.id")
            ->groupBy("items.id")
            ->having("total_quantity","<",DB::raw("items.min_quantity"))
            ->get();
print_r($items);
```

You will find bellow output get only items no. 2.

Read Also: [How to use groupby having with DB::raw in Laravel Query Builder?](https://www.itsolutionstuff.com/post/how-to-use-groupby-having-with-dbraw-in-laravel-query-builderexample.html)

```
Array

(

    [0] => stdClass Object

        (

            [id] => 2

            [title] => Itsolutionstuff.com

            [min_quantity] => 10

            [total_quantity] => 5

        )

)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/laravel-advanced/how-to-use-groupby-having-with-db-raw-in-laravel-query-builder-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
