0

Alreay i sum the column price groupby inventory_id .Additionally i need to calculate total price.

$record = ProductDetails::with('inventoryName')->whereDate('created_at', '=', date($date))
      ->select('inventory_id',DB::raw('sum(price) as item_price'),DB::raw('sum(quantity) as quantity'))->groupBy('inventory_id')->get();

My output is :

[
  {
    inventory_id: 9,
    item_price: 30,
    quantity: 30,
    inventory_name: [
      {
        id: 9,
        name: "sugar"
      }
    ]
  },
  {
    inventory_id: 10,
    item_price: 70,
    quantity: 70,
    inventory_name: [
      {
        id: 10,
        name: "oil"
      }
    ]
  }
]

Now i need to calculate total price.How to do this.

kirubha
  • 123
  • 6

1 Answers1

1

You can use Laravel Collection: SUM method.

1. If $record is already a collection:
echo $record->sum('item_price');

2. If $record is an array.
$collection = collect($record);
$collection->sum('item_price');
Dark Knight
  • 6,116
  • 1
  • 15
  • 37