Hello.
I'm currently working on displaying the income for an e-commerce site the past 7 days in a chart.
Orders are stored in a table called orders.
Orders contains a column called itemID referring to a table called items.
Items table contains a price column.
Each order can only have one item.
My order model has a function like this
public function item(){
return $this->belongsTo(Item::class);
}
So to calculate the income per day, would the best solution be retrieving all orders past 7 days from the db, then loop through them and do something like
$incomeLastWeek[$order->created_at] += $order->item->price;
What are your opinions? What do you think would be the best solution here?
Thank You!
I suggest using a reports or summary table. I fire scheduled commands (commands so I can run them on the fly too) to store reports.
This way you get expected data, quickly and efficiently with out running expensive queries every page load (that ultimately puts out the same data daily).
Matter of fact I have a two year old app that uses lumen just for reports and analytic data in reports tables. Json might be a good fit to save as, allows easy adjustments to code with out messing with table structure later and most charts use json anyways.