Laravel 5 search records 30min and older for today

2019-07-25 10:21发布

问题:

I am trying to get all records that are 30min old and are today with a field called smsed value = to 0.

What i am trying to do is get all the records in my database with todays date and are older than 30min.

$data = DB::table('applicant')->whereRaw('AppDate < (NOW() - INTERVAL 30 MINUTE)')->where('smsed','=',0)->limit(5000)->get();

what the above does is get all records in the DB and not only for today.

回答1:

This is because you only asking it for records that are over 30minutes old and not including anything to limit it to today.

You could use something like whereBetween:

$data = DB::table('applicant')
    ->whereBetween('AppDate', [Carbon\Carbon::now()->startOfDay(), Carbon\Carbon::now()->subMinute(30)])
    ->where('smsed', '=', 0)
    ->limit(5000)
    ->get();

Alternatively, if you just want to keep your sql functions you could do something like:

$data = DB::table('applicant')
    ->whereRaw('AppDate < (NOW() - INTERVAL 30 MINUTE)')
    ->whereRaw('DATE(AppDate) = CURDATE()')
    ->where('smsed','=',0)
    ->limit(5000)
    ->get();

Hope this helps!



标签: laravel-5