Get data by date in laravel

In the database i have data : table : product

id   name     order      created_ad
1   Iphone     3       2019-01-12 06:17:26
2   Samsung    2       2019-01-12 04:05:30
3   Xiaomi     1       2019-01-12 09:07:02
4   Master     4       2020-06-05 09:17:12
5   Macbook    3       2020-08-11 14:04:20

I controller ProductController.php
public function index()
  $product = Product::select('id', 'name', 'created_at')->where('created_at', '2019-01-12')->get()->toArray();

The return result is an empty array !
Now I want to get all the data of the date 2019-01-12, how do I do that?. Thanks.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use whereDate(). Create at is date time format, but you want to get data by date only so you need to update you code.

public function index()
  $product = Product::select('id', 'name', 'created_at')->whereDate('created_at', '2019-01-12')->get()->toArray();

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x