Amrinder Singh
Amrinder Singh

Reputation: 5532

Get record from database inserted in last two hours using Laravel 5.1

I want to get all the record from the database inserted in last two hours in Laravel 5.1. I am able to get the record for last 1 hour by using the following code:

$all_bridal_requests_check2 = \DB::table('bridal_requests')
                    ->where(function($query)
                    {
                        $query->where('publisher', '=', 'bq-quotes.sb.com')
                              ->orWhere('publisher', '=', 'bq-wd.com-bsf');
                    })
                    ->where('created_on', '>=', \Carbon\Carbon::now()->subHour())
                    ->orderBy('created_on', 'desc')
                    ->get();

Upvotes: 1

Views: 1626

Answers (3)

Amarnasan
Amarnasan

Reputation: 15579

Instead of "subHour", use "subHours(2)"

->where('created_on', '>=', \Carbon\Carbon::now()->subHours(2))

Upvotes: 1

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

use MySQL NOW() or INTERVAL

WHERE `created_on` > NOW() - INTERVAL 2 HOUR

in Laravel

->where('created_on', '>', NOW() - INTERVAL 2 HOUR)

Upvotes: 1

Saani
Saani

Reputation: 782

Try this:

$all_bridal_requests_check2 = \DB::table('bridal_requests')
                    ->where(function($query)
                    {
                        $query->where('publisher', '=', 'bq-quotes.sb.com')
                              ->orWhere('publisher', '=', 'bq-wd.com-bsf');
                    })
                    ->where(\DB::raw('date >= DATE_SUB(NOW(), INTERVAL 2 HOUR)')) ->count()
                    ->orderBy('created_on', 'desc')
                    ->get();

Upvotes: 0

Related Questions