How To Use where clause On a withCount Method Using Laravel?

10-Mar-2023

.

Admin

How To Use where clause On a withCount Method Using Laravel?

Hello Dev,

Now let's see example of how to use where clause on a withCount method example. We will check how to use where clause on a withCount method. This is a short guide on where clause on a withCount method in laravel.Let's get started with how to where clause on a withCount method in laravel.

Here i will give you many example how to use where clause on a withCount method using laravel.

Simple Count Example


$categories = Category::withCount('articles')->get();

//articles_count

Multiple Counts Example

$categories = Category::withCount(['articles','products'])->get();

//articles_count, products_count

Count With Condition Example

$categories = Category::withCount(['articles' => function ($query) {

$query->where('status', 1);

}])->get();

//articles_count

It will help you...

#Laravel