diff --git a/app/Models/Barrio.php b/app/Models/Barrio.php index 6319d37..096693b 100644 --- a/app/Models/Barrio.php +++ b/app/Models/Barrio.php @@ -117,20 +117,17 @@ private function armarColumnasPedido() : array { private function productosCantidadesPedidas() : array { return $this->pedidos() ->where('confirmed',true) - ->with('products') + ->with('productos') ->get() - ->flatMap(fn($order) => $order->products->map( - fn($product) => [ - 'product_id' => $product->id, - 'product_name' => $product->name, - 'total_ordered' => $product->pivot->quantity, + ->flatMap(fn($pedido) => $pedido->productos->map( + fn($producto) => [ + 'producto_id' => $producto->id, + 'cantidad' => $producto->pivot->cantidad, ] )) - ->groupBy('product_id') - ->map(fn($groupedProducts) => [ - 'product_id' => $groupedProducts->first()['product_id'], - 'product_name' => $groupedProducts->first()['product_name'], - 'total_ordered' => $groupedProducts->sum('total_ordered'), + ->groupBy('producto_id') + ->map(fn($productosAgrupados) => [ + $productosAgrupados->first()['producto_id'] => $productosAgrupados->sum('cantidad'), ]); } }