Laravel - Laravel Eloquent Methods withSum(),withMin(),withMax(),withAvg(),withCount()

Published on Aug 18, 2022

Hello Artisans, today I'll show you some important methods of Laravel eloquent. These are withSum(),withMin(),withMax(),withAvg(). These 4 methods are easy to use yet handy to use. Let's see how we can use in our Laravel Application.

First of all, suppose we've a Category.php and Product.php model. And the relation between category and product is hasMany(). So, we've the following setup.

app/Models/Category.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    protected $fillable = ['title','image','status'];

    public function products(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Product::class);
    }
}
app/Models/Product.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    public function category(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Category::class);
    }
}

So, according to above setup we can use the below example

Example 1 : withSum()

In first example we'll see the usage of withSum(). Using withSum(), we can get the sum of the any column which is in relation. For example, see the below example.

public function index()
    {
        return Category::withSum('products','price')->get();
    }

Here what we do is, we will sum the price of products table. Let's look at the output

[
  {
    "id": 1,
    "title": "Electronics",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_sum_price": 15
  }
]

Example 2 : withMin()

Now we'll see the usage of withMin(). See the below code snippet.

public function index()
    {
        return Category::withMin('products','price')->get();
    }

Here what we do is, we will find the minimum price of products table. Let's look at the output

[
  {
    "id": 1,
    "title": "Electronics",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_min_starting_price": 15
  },
  {
    "id": 2,
    "title": "Toys",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_min_starting_price": 25
  }
]

Example 3 : withMax()

Now we'll see the usage of withMax(). See the below code snippet.

public function index()
    {
        return Category::withMax('products','price')->get();
    }

Here what we do is, we will find the maximum price of products table. Let's look at the output

[
  {
    "id": 1,
    "title": "Electronics",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_max_price": 15
  },
  {
    "id": 2,
    "title": "Toys",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_max_price": 35
  }
]

Example 4 : withAvg()

Now we'll see the usage of withAvg(). See the below code snippet.

public function index()
    {
        return Category::withAvg('products','price')->get();
    }

Here what we do is, we will find the average price of products table. Let's look at the output

[
  {
    "id": 1,
    "title": "Electronics",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_avg_price": 15
  },
  {
    "id": 2,
    "title": "Toys",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_avg_price": 25
  }
]

Example 5 : withCount()

Now we'll see the usage of withCount(). See the below code snippet.

public function index()
    {
        return Category::withCount('products')->get();
    }

Here what we do is, we will count the number of products in the table. Let's look at the output

[
  {
    "id": 1,
    "title": "Electronics",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_count": 2
  },
  {
    "id": 2,
    "title": "Toys",
    "image": "public/uploads/images/b7ba6be9-10b3-4c0d-9248-545aeddf762a.jpg",
    "status": 1,
    "created_at": "2022-08-15T17:36:11.000000Z",
    "updated_at": "2022-08-15T17:40:28.000000Z",
    "products_count": 2
  }
]

That's it for today. Hope you'll enjoy through this tutorial. Catch me in the comment section if you face any kind of problem. Thanks for reading :)

Comments (0)