Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching rows added in the last hour using Laravel 5.1

I am working on a project in which I have to track all the records which are added in last hour of current time.

I am able to do this by using following query,

select count(*) as cnt
from  log
where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR); 

but not able to do the same in Laravel.

like image 605
Amrinder Singh Avatar asked Oct 25 '25 20:10

Amrinder Singh


1 Answers

You can use the Carbon date time api to get the last hour formated datetime

$count = \DB::table('logs')->where('date', '>=', \Carbon\Carbon::now()->subHour())->count();
like image 156
Ahmed Zekry Avatar answered Oct 28 '25 09:10

Ahmed Zekry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!