Reputation: 131
I'm studing sum in Laravel. I woud like to display "total price" of the First week , the second week, the third week, the fourth week diffrently. And I also display This month's total price amount.
Could you teach me how to write controller code please?
public function total_price_amount_this_month()
{
$prices= PriceList::orderBy('id', 'desc')->get();
return view('total_price_amount_this_month',compact('prices'));
}
public function the_first_week()
{
$prices= PriceList::orderBy('id', 'desc')->get();
return view('the_first_week',compact('prices'));
}
Here is my table.
Upvotes: 1
Views: 898
Reputation: 913
If you want to calculate the sum prices of latest 7 days you can do like this :
$price = PriceList::whereDate('time', Carbon::now()->subDays(7))
->sum('price');
print_r($price);
Or if you want to put the dates manually the use whereBetween()
Hope it will help u!
Edit: If want it manually
$from_date = //Whatever
$to_date = //Whatever
$price = PriceList::whereBetween('time', [$from_date, $to_date])
->sum('price');
Thats what you have to do!
Upvotes: 2