Reputation: 202
in approve_date
I have date stored in this form 2021-04-28 20:03:00
and i want to fetch all the record from passing 2021-04-28
so i tried this piece of code
Order::whereDate('approve_date' , 2021-04-28)->get()->sum('subtotal');
but the problem is i am getting 0 and in db subtotal(sum) is 700 of 3 rows
Upvotes: 3
Views: 215
Reputation: 68
Try this please:
Order::whereDate('approve_date', '=', new Carbon('2021-04-28'))->get()->sum('subtotal')
I recomend to use DateTime object or Carbon.
PS don't forget to use use Carbon\Carbon;
Upvotes: 1
Reputation: 117
Because the date format u stored in database is 2021-04-28 20:03:00 format ,
To fetch the record with only YYYY-M-D , you first need to parse the DATE to
Order::whereDate('DATE(approve_date)' , 2021-04-28)->get()->sum('subtotal');
Upvotes: 0