Laravel Eloquent Withsum() and Withcount() Tutorial Example
Teams applying “Laravel Eloquent Withsum() and Withcount() Tutorial Example” in a production project can use work life integration to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hello Friends,
Today, I will let you know an example of laravel's eloquent withsum() and withcount() tutorial example. This article will give you a simple example of the laravel sum relationship column. I explained simply about laravel withSum() example. We will use laravel eloquent withsum.
Here i will give you very simple example of how to use withSum() and withCount() with laravel relationship eloquent. we will create Category and Product and how you can add relationship and get it.
You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.
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'
];
}
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] => 330
)
[1] => Array
(
[id] => 2
[name] => Laptop
[products_sum_price] => 410
)
)
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
)
)
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial