Yousef Altaf
Yousef Altaf

Reputation: 2763

laravel-5.4 : get records that is created_at from yesterday at 5 o'clock until now

I need to get the records that is created from yesterday at 5.00PM until now so if I try the following:

... and so on

Here is my function.

public function allNewOrdersToday() {
        $allNewOrders = ( new OrderList() )
            ->where( 'created_at', '>=', Carbon::yesterday() )

        return $allNewOrders;
    }

Upvotes: 1

Views: 6033

Answers (2)

thisiskelvin
thisiskelvin

Reputation: 4202

Using Carbon, you should be able to query where the date is beyond Yesturday 5:00pm using the yesturday() and setTime() carbon methods:

use Carbon\Carbon;

... 

public function allNewOrdersToday() {
    return OrderList::whereDate('created_at', '>=', Carbon::yesterday()->setTime(17, 00, 00)->toDateTimeString())
        ->get();
}

In this example, I have changed (new OrderList()) to OrderList::... as you are using eloquent models and are able to call query builder methods without needing a new instance of the class.

Upvotes: 3

Mohammad Wasim
Mohammad Wasim

Reputation: 199

Try to do like this:

public function allNewOrdersToday() {
    $allNewOrders = ( new OrderList() )
       ->whereDate( 'created_at', '>=', date('Y-m-d 17:00:00',strtotime("-1 days")) 
    return $allNewOrders;
 }

Upvotes: 1

Related Questions