Laravel 10 withCount() with Where Condition Tutorial Example

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 ) )
I hope it can help you...
Divyang Vadodariya
My name is Divyang Vadodariya. I'm a full-stack developer, entrepreneur and owner of RvSolutionStuff. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap ,etc from the early stage.

We are Recommending you
- Laravel 9 One To One Relationship Tutorial With Example
- Laravel 9 Store Log Of Eloquent SQL Queries
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Google Maps Multiple Markers Example
- How to Http Curl Delete Request in Laravel?
- How to Get Array of Ids from Eloquent Models in Laravel?
- Many to Many Eloquent Relationship Laravel Tutorial
- One to Many Polymorphic Eloquent Relationship Laravel Toturial
- Laravel Many to Many Polymorphic Relationship Tutorial
- Laravel 10 withCount() with Where Condition Tutorial Example
Copyright © 2023 www.RVSolutionStuff.com • All Rights Reserved