ItSolutionStuff.com

Laravel withCount() with Where Condition Example

By Hardik Savani β€’ April 16, 2024
Laravel

Hello Developer,

In this short tutorial, we will share the quick and straightforward way to laravel withcount with condition. let’s discuss about laravel withcount where condition. you will learn laravel withcount condition. I explained simply step by step laravel relationship withcount with condition.

Here i will give you very simple example of how to use withCount() with laravel relationship eloquent. i will also give you example of withCount() with where condition. here we will create Category and Product and how you can add relationship and get it.

you can easily use withCount() with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

Let's see example with output:

Category Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Category extends Model

{

use HasFactory;

/**

* Get the comments for the blog post.

*/

public function products()

{

return $this->hasMany(Product::class);

}

}

Product Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Product extends Model

{

use HasFactory;

protected $fillable = [

'name', 'price', 'is_active'

];

}

withCount() Example:

<?php

namespace App\Http\Controllers;

use App\Models\Category;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$categories = Category::select("id", "name")

->withCount('products')

->get()

->toArray();

dd($categories);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Mobile

[products_count] => 3

)

[1] => Array

(

[id] => 2

[name] => Laptop

[products_count] => 2

)

)

withCount() with Where Condition Example:

<?php

namespace App\Http\Controllers;

use App\Models\Category;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$categories = Category::select("id", "name")

->withCount([

'products as active_products_count' => function ($query) {

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

},

])

->get()

->toArray();

dd($categories);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Mobile

[active_products_count] => 2

)

[1] => Array

(

[id] => 2

[name] => Laptop

[active_products_count] => 2

)

)

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Laravel Where Clause with Function Query Example

Read Now β†’
β˜…

Laravel Sum Query with Where Condition Example

Read Now β†’
β˜…

Laravel Eloquent whereRelation() Condition Example

Read Now β†’
β˜…

Laravel Eloquent whereHas() Condition Example

Read Now β†’
β˜…

Laravel Eloquent Left Join Where Null Condition Example

Read Now β†’
β˜…

Laravel Eloquent withSum() and withCount() Example

Read Now β†’
β˜…

Laravel Eloquent Relationships Tutorial From Scratch

Read Now β†’
β˜…

Laravel One to Many Eloquent Relationship Tutorial

Read Now β†’
β˜…

Laravel Many to Many Eloquent Relationship Tutorial

Read Now β†’
β˜…

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now β†’
β˜…

Laravel - whereDate(), whereMonth(), whereDay() and whereYear() Example

Read Now β†’
β˜…

Laravel Where Condition with Two Columns Example

Read Now β†’
β˜…

Laravel Where Clause with date_format() Example

Read Now β†’
β˜…

Laravel Eloquent Where Like Query Example Tutorial

Read Now β†’
β˜…

Laravel Where Clause with MySQL Function Example

Read Now β†’