Amrinder Singh
Amrinder Singh

Reputation: 5492

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.

Upvotes: 4

Views: 5757

Answers (4)

Dean Gite
Dean Gite

Reputation: 1698

Db::raw

$count = \DB::table('log') ->where(\DB::raw('date >= DATE_SUB(NOW(), INTERVAL 1 HOUR)')) ->count();

Upvotes: 1

Jerodev
Jerodev

Reputation: 33186

You can use DB::raw() to execute special query parts.

Like so:

$count = \DB::table('log')
    ->where('date', '>=', \DB::raw('DATE_SUB(NOW(), INTERVAL 1 HOUR)'))
    ->count();

Upvotes: 0

Ahmed Zekry
Ahmed Zekry

Reputation: 106

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();

Upvotes: 9

Snehal S
Snehal S

Reputation: 875

you can do it with this

\DB::table('log')->where(['date' => DB::raw('DATE_SUB(NOW(),INTERVAL 1 HOUR)')])->count();

Upvotes: 0

Related Questions