1

Are there any way to get difference of two days (today & created_at) using laravel query?

I have tried as follows.but that didn't work for me

public function scopeActivatedOrders($query,$currentDate){
        return $query->select('*')
            ->where('orders.activated', '=', '1')
            ->where('DATEDIFF(order.arrived_date,$currentDate)','>','14')->get();
    }

Thank you!

  • Does this answer your question? [Calculate difference between two dates with timestamps in Laravel](https://stackoverflow.com/questions/58957324/calculate-difference-between-two-dates-with-timestamps-in-laravel) – Kamlesh Paul Dec 17 '20 at 03:41
  • I want to get the difference of two days using query – Jounior Developer Dec 17 '20 at 04:12

2 Answers2

2

the problem in your code is that you are using standard where that take a column as the first parameter not an expression ... you can use whereRaw, or just using DB::raw like:

return $query->select('*')
            ->where('orders.activated', '=', '1')
            ->whereRaw('DATEDIFF(order.arrived_date,Now())>14')->get();

note: you can mysql now() function instead of $currentDate variable ...

OMR
  • 11,736
  • 5
  • 20
  • 35
0

try this 1st select then add in where condition

public function scopeActivatedOrders($query, $currentDate)
    {
        return $query->select('orders.*', \DB::raw('DATEDIFF(order.arrived_date, NOW()) as diffday'))
            ->where('orders.activated', '=', '1')
            ->get()
            ->where('diffday', '>', '14');
    }
Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33