Laravel Query Builder - Where date is now using carbon
Asked Answered
E

1

10

How to get only date on database column, I have difficulties using Carbon on controller:

$data['nowUser'] = User::where('date', Carbon::today()->toDateString())->get();

Date column looks like this in the database:

enter image description here

Examination answered 13/12, 2015 at 1:45 Comment(0)
M
21

That is a DATETIME column, so there's no need for additional formatting the Carbon instance. However you need to use whereDate if you want the fetch all users for which the date column contains today's date:

$data['nowUser'] = User::whereDate('date', '=', Carbon::today())->get();

Because when you pass Carbon::today() to the Query Builder method, the __toString method will be automatically called and return a DATETIME string with the format from Carbon::DEFAULT_TO_STRING_FORMAT, which is exactly that MySQL format Y-m-d H:i:s.

Malvaceous answered 13/12, 2015 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.