Reputation: 5492
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
Reputation: 1698
Db::raw
$count = \DB::table('log') ->where(\DB::raw('date >= DATE_SUB(NOW(), INTERVAL 1 HOUR)')) ->count();
Upvotes: 1
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
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
Reputation: 875
you can do it with this
\DB::table('log')->where(['date' => DB::raw('DATE_SUB(NOW(),INTERVAL 1 HOUR)')])->count();
Upvotes: 0