Naim Fauzi
Naim Fauzi

Reputation: 55

Changing Carbon object date format to be used for query

I have a table that consists of a column named created_at. Whenever I dump this specific column, it returns as a Carbon object as below: enter image description here

I've coded this query to get data from the said table based on date. Code is as below:

$date = $this->request->get('date');
$orders = Order::where( 'created_at', $date )->get();

$date is retrieved from the url and the format is 'Y-m-d' making it impossible to query properly since 'created_at' and $date is not in the same format. How can I get date from the Carbon object in 'Y-m-d' format and insert it in my query?

Upvotes: 0

Views: 1661

Answers (2)

Yann1ck
Yann1ck

Reputation: 123

This should do the job

$date = $date->toDateString();      

Or just

$date = $date->format("Y-m-d");

Upvotes: 0

Kotzilla
Kotzilla

Reputation: 1413

if $date is in format Y-m-d you can use whereDate like this

$orders = Order::whereDate( 'created_at', $date )->get();

Upvotes: 5

Related Questions