In this tutorial you will learn about the Laravel Eloquent withSum() and withCount() Tutorial and its application with practical example.
In this Laravel Eloquent withSum() and withCount() tutorial, I’ll show you how to use laravel eloquent withcount and withsum() method. In this article you also learn about the use case of laravel withSum() and withCount() methods.
Laravel Eloquent withSum() and withCount() Tutorial
In this step by step tutorial you lean about the laravel withcount and withsum() with different example.
Category Model:
Let’s create a category model and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?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:
Now, we will create a product model and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?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' ]; } |
Laravel relationship with withSum() Example:
In this example we will use laravel relationship with withSum() method as following:
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php namespace App\Http\Controllers; use App\Models\Category; class MyController extends Controller { /** * Write code on Method * * @return response() */ public function index() { $categories = Category::select("id", "name") ->withSum('products', 'price') ->get() ->toArray(); dd($categories); } } |
Result:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Array ( [0] => Array ( [id] => 1 [name] => Mobile [products_sum_price] => 330 ) [1] => Array ( [id] => 2 [name] => Laptop [products_sum_price] => 410 ) ) |
Laravel relationship with withCount() Example:
In this example we will use laravel relationship with laravel withCount() method as following:
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php namespace App\Http\Controllers; use App\Models\Category; class MyController extends Controller { /** * Write code on Method * * @return response() */ public function index() { $categories = Category::select("id", "name") ->withCount('products') ->get() ->toArray(); dd($categories); } } |
Result:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Array ( [0] => Array ( [id] => 1 [name] => Mobile [products_count] => 3 ) [1] => Array ( [id] => 2 [name] => Laptop [products_count] => 2 ) ) |