Laravel withSum() with Where Condition Example
Hi Developer,
This is a short guide on laravel withsum with condition. you will learn laravel withsum where condition. We will look at an example of laravel withsum condition. This post will give you a simple example of laravel relationship withSum with condition. Alright, let us dive into the details.
Here i will give you very simple example of how to use withSum() with laravel relationship eloquent. i will also give you example of withSum() with where condition. here we will create Category and Product and how you can add relationship and get it.
you can easily use withSum() with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
Let's see example 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', 'is_active'
];
}
withSum() 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")
->withSum('products', 'price')
->get()
->toArray();
dd($categories);
}
}
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => Mobile
[products_sum_price] => 30
)
[1] => Array
(
[id] => 2
[name] => Laptop
[products_sum_price] => 20
)
)
withSum() 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")
->withSum([
'products' => function ($query) {
$query->where('is_active', '1');
}], 'price')
->get()
->toArray();
dd($categories);
}
}
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => Mobile
[products_sum_price] => 20
)
[1] => Array
(
[id] => 2
[name] => Laptop
[products_sum_price] => 20
)
)
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. 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, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Check If Relationship Data is Empty Example
- Laravel Mail Send with PDF Attachment Example
- Laravel Where Clause with Function Query Example
- Laravel Sum Query with Where Condition Example
- Laravel Eloquent whereRelation() Condition Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Eloquent Left Join Where Null Condition Example
- Laravel Relationship Eager Loading with Count Example
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel One to One Eloquent Relationship Tutorial
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel Where Condition with Two Columns Example