Skip to content

All topics  /  Laravel

Laravel 10 withCount() with Where Condition Tutorial

Laravel ·

Teams applying “Laravel 10 withCount() with Where Condition Tutorial” in a production project can use this detailed guide to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hi dev,

In this little tutorial, we'll explain how to use a laravel withcount and condition quickly and easily. Let's talk about laravel where condition and withcount. Laravel will be taught to you with count conditions. I described the Laravel link between count and condition clearly and step by step.

Here, I'll demonstrate how to use withCount() with Laravel relationship elegantly with a very basic example. I'll also provide an example of using where and withCount(). Here, we'll explain how to build a category, a product, and how to add a relationship to obtain it.

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

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
        )
)