I’m trying to display a data by month and displays the product with the highest value or bought quantity. But I don’t know how I should do it.
This is the code I have and tried so far
public function viewSales(){
$data = sales::select(
[sales::raw("SUM(product_fee) as product_fee, MONTHNAME(created_at) as month_name, MAX(bought) as bought"),'product']
)->whereYear('created_at', date("Y"))
->orderBy('created_at', 'asc')
->groupBy('month_name')
->get();
return view('admin.viewSales', compact('data'));
}
This is what’s inside my database
id | product | bought | product_fee | created_at |
---|---|---|---|---|
1 | Dictionary | 1 | 200 | 2023-01-14 18:55:34 |
2 | Horror | 3 | 100 | 2023-01-15 17:55:34 |
3 | How to cook | 5 | 300 | 2023-01-16 11:55:34 |
and this is what I’m trying to display
Most bought product | sold | overall earning |
---|---|---|
How to cook | 5 | 600 |
2
Answers
i think you can try this :
I hope this works for you
Below SQL will give you the desired output for current year and month.
Fetch for current year
Fetch for months of the current year
SQL Fiddle