Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve data of current date in Laravel

Tags:

sql

php

laravel

I want to retrieve data of today from my database in Laravel.

I have already googling and applied several methods but not getting my answer.

PrescriptionTbl::whereDate('created_at', '=', $today)->get();

Where PrescriptionTbl is the model and today is current date like 2019-2-19.But got NULL array.

To check the SQL I have used toSQL() instead of get()...and it shows below SQL in the browser

 select * from `prescription_tbls` where date(`created_at`) = ?

I have also use Carbon but also get a NULL array

PrescriptionTbl::whereDate('created_at', Carbon::today())->get();

How to solve it? Anybody Help Please?

like image 578
Arafat Rahman Avatar asked Jan 01 '23 06:01

Arafat Rahman


2 Answers

You can use that;

PrescriptionTbl::whereDate('created_at', date('Y-m-d'))->get();
like image 83
FGDeveloper Avatar answered Jan 03 '23 20:01

FGDeveloper


You can get it via new DateTime() class

PrescriptionTbl::whereDate('created_at', '=', (new DateTime)->format('Y-m-d'))->get();
like image 35
Pascal Tovohery Avatar answered Jan 03 '23 20:01

Pascal Tovohery