Reputation: 707
i'm making report by month i need to get product how many it was sold this month with quantity My Code Returning all products but how i sum the same product so in example
{
product_id: 1,
qty: 2,
line_total: 120,
created_at: "2020-07-30T12:06:04.000000Z"
},
{
product_id: 1,
qty: 2,
line_total: 120,
created_at: "2020-07-30T12:06:04.000000Z"
},
i need to return it like this
{
product_id: 1,
qty: 4,
line_total: 240,
created_at: "2020-07-30T12:06:04.000000Z"
}
My code
$product_orders = \App\OrderProduct::select('product_id','qty','line_total','created_at')
->orderBy('created_at')->get();
$product_orders->groupBy(function ($item, $key) {
return $item->created_at->format('m');
});
return $product_orders;
Upvotes: 0
Views: 196
Reputation: 1019
Try this code... Hope it works
Use DB;
$products= OrderProduct::select(
DB::raw('MONTHNAME(created_at) as month'),
DB::raw('YEAR(created_at) as year'),
DB::raw('DAY(created_at) as day'),DB::raw('sum(line_total) as total_line') , 'product_id','qty','line_total','created_at')->groupBy('year', 'month','day')->get();
Upvotes: 1