Laravel Eloquent withMin(), withMax() and withAvg() Example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

This tutorial is focused on laravel eloquent withAvg(). if you want to see lesson of laravel eloquent withMax() then you are a right place. i explained simply step by step laravel eloquent withMin(). We will look at lesson of laravel with max eloquent. So, let's follow few step to create lesson of laravel withmin.

Here i will give you very simple lesson of how to use withMin(), withMax() and withAvg() with laravel relationship eloquent. we will create Category and Product and how you can add relationship and get it.

you can easily use withMin(), withMax() and withAvg() with laravel 6, laravel 7 and laravel 8 version.

Let's see lesson 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'

];

}

withMin() 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")

->withMin('products', 'price')

->get()

->toArray();

dd($categories);

}

}

Result:-

Array

(

[0] => Array

(

[id] => 1

[name] => Mobile

[products_min_price] => 100

)

[1] => Array

(

[id] => 2

[name] => Laptop

[products_min_price] => 200

)

withMax() 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")

->withMax('products', 'price')

->get()

->toArray();

dd($categories);

}

}

Result:-

Array

(

[0] => Array

(

[id] => 1

[name] => Mobile

[products_max_price] => 120

)

[1] => Array

(

[id] => 2

[name] => Laptop

[products_max_price] => 210

)

)

withAvg() 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")

->withAvg('products', 'price')

->get()

->toArray();

dd($categories);

}

}

Result:-

Array

(

[0] => Array

(

[id] => 1

[name] => Mobile

[products_avg_price] => 110.0000

)

[1] => Array

(

[id] => 2

[name] => Laptop

[products_avg_price] => 205.0000

)

)

thank you very much for following up with me.

Recent Posts

Related Posts