diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index c648a487..13352f36 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -32,31 +32,35 @@ public function index() $low_stock_products = Product::where('quantity', '<', 10)->get(); $bestSellingProducts = DB::table('products') - ->select('products.*', DB::raw('SUM(order_items.quantity) AS total_sold')) - ->join('order_items', 'order_items.product_id', '=', 'products.id') - ->join('orders', 'orders.id', '=', 'order_items.order_id') - ->groupBy('products.id') - ->havingRaw('SUM(order_items.quantity) > 10') - ->get(); + ->select('products.id', 'products.name', DB::raw('SUM(order_items.quantity) AS total_sold')) + ->join('order_items', 'order_items.product_id', '=', 'products.id') + ->join('orders', 'orders.id', '=', 'order_items.order_id') + ->groupBy('products.id', 'products.name') + ->havingRaw('SUM(order_items.quantity) > 10') + ->get(); + + $currentMonthBestSelling = DB::table('products') - ->select('products.*', DB::raw('SUM(order_items.quantity) AS total_sold')) - ->join('order_items', 'order_items.product_id', '=', 'products.id') - ->join('orders', 'orders.id', '=', 'order_items.order_id') - ->whereYear('orders.created_at', date('Y')) - ->whereMonth('orders.created_at', date('m')) - ->groupBy('products.id') - ->havingRaw('SUM(order_items.quantity) > 500') // Best-selling threshold for the current month - ->get(); + ->select('products.id', 'products.name', 'products.price', DB::raw('SUM(order_items.quantity) AS total_sold')) + ->join('order_items', 'order_items.product_id', '=', 'products.id') + ->join('orders', 'orders.id', '=', 'order_items.order_id') + ->whereYear('orders.created_at', date('Y')) + ->whereMonth('orders.created_at', date('m')) + ->groupBy('products.id', 'products.name', 'products.price') // Add all selected columns here + ->havingRaw('SUM(order_items.quantity) > 500') + ->get(); + $pastSixMonthsHotProducts = DB::table('products') - ->select('products.*', DB::raw('SUM(order_items.quantity) AS total_sold')) - ->join('order_items', 'order_items.product_id', '=', 'products.id') - ->join('orders', 'orders.id', '=', 'order_items.order_id') - ->where('orders.created_at', '>=', now()->subMonths(6)) // Filter for the past 6 months - ->groupBy('products.id') - ->havingRaw('SUM(order_items.quantity) > 1000') // Hot product threshold for past 6 months - ->get(); + ->select('products.id', 'products.name', 'products.price', 'products.created_at', DB::raw('SUM(order_items.quantity) AS total_sold')) + ->join('order_items', 'order_items.product_id', '=', 'products.id') + ->join('orders', 'orders.id', '=', 'order_items.order_id') + ->where('orders.created_at', '>=', now()->subMonths(6)) // Filter for the past 6 months + ->groupBy('products.id', 'products.name', 'products.price', 'products.created_at') // Add all selected columns here + ->havingRaw('SUM(order_items.quantity) > 1000') // Hot product threshold for past 6 months + ->get(); +