Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hala's avatar
Level 3

query on created_at with carbon

I have this query

          Community::where('created_at', Carbon::now()->subDays(3)->toDateString())

and in db i have this raw "created_at" : ISODate("2020-10-14T16:26:20.762Z")

but each time i made query data return empty

0 likes
8 replies
kingmaker_bgp's avatar

@hala, If you need to query Community created specific days before, you can use the ::whereDate function

Community::whereDate('created_at', Carbon::now()->subDays(3))
Hala's avatar
Level 3

i tried it before i'm not using SQL so whereDate not implement in it

Hala's avatar
Level 3

Mongo , and Jenssegers as a package

Snapey's avatar
Snapey
Best Answer
Level 122

this

Community::where('created_at', Carbon::now()->subDays(3)->toDateString())

doesn't actually do anything because you need to actuall run the query you created.

The query is executed with get() if you expect a collection, or first() if you only want one answer

you dont need to use the toDateString since eloquent handles carbon objects directly

$records=Community::where('created_at', now()->subDays(3))->get();

bear in mind that this query uses times, so actually the time of day the record was created is also considered

Hala's avatar
Level 3

@snapey thank you for your answer yes i know about get and first i use them but i copy only the query in specific

actually after searching i got into this

      Community::where('created_at', '<',Carbon::now()->subDays(3))->pluck('_id', 'is_closed');
Snapey's avatar

how can we help then if you show things out of context and dont include relevant information?

Please or to participate in this conversation.