Get data by date in laravel

Viewed 1245

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();
  dd($product);
}

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.

1 Answers

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();
  dd($product);
}
Related