public function getAll() { $businesses = Business::with(['orders.orderItems.product', 'orders.invoices', 'orders.customer'])->get(); $stock = 0; foreach ($businesses as $business) { if ($business->name == "Nhập hàng") { $inStock = 0; foreach ($business->orders as $order) { foreach ($order->orderItems as $orderItem) { $inStock += $orderItem->quantity * $orderItem->product->cost; } } } if ($business->name == "Xuất hàng") { $outStock = 0; foreach ($business->orders as $order) { foreach ($order->orderItems as $orderItem) { $outStock += $orderItem->quantity * $orderItem->product->cost; } } } } $stock = $inStock - $outStock; array_add($businesses, 'stock', $stock); return Response::json($businesses); }