3

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

2 Answers 2

1

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;

Sign up to request clarification or add additional context in comments.

Comments

-1

Because the date format you stored in database is 2021-04-28 20:03:00 format.

To fetch the record in YYYY-M-D, you first need to parse the DATE to

Order::whereDate('DATE(approve_date)' , 2021-04-28)->get()->sum('subtotal');

2 Comments

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DATE(approve_date)' in 'where clause' (SQL: select * from orders where date(DATE(approve_date)) = 1989) getting thi serror
for this to work you will have to make a raw statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.