Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();




Expand Down