abkrim
abkrim

Reputation: 3692

Select with advanced where clauses and timestamp using Carbon and DB

I have a code to generate a SQL query from a table. I want to select items that exist between dates and a true value in another field.

DB use in construction and Carbon facades, following advice on how to work with Carbon in laravel 5.

But I do not get the effect I returns all rows

private function runBids() {
      $dt = Carbon::parse(Config::get('constants.start_lot'));
      $start = $dt->toDateTimeString();  // 2006-05-08 08:34:59
      $end   = $dt->addDay()->startOfDay(); // 2006-05-09 00:00:00

      $lots = DB::table('lots')
               ->select('id')
               ->where('end',false)
               ->where('end_auction', '<=', $end)
               ->where('end_auction', '=>', $start)  // Not work. Return 0 results
     // if comment  ->where('end_auction', '=>', $start) result 39 results with date 
     // between dates 
     // (start it's date of first element of table order by end_auction) 
               ->get();
      $lots_id = array();
      foreach ($lots as $value){
         $lots_id[] = $value->id;
      }
      dd($lots_id);
}

Upvotes: 0

Views: 592

Answers (1)

Lu&#237;s Cruz
Lu&#237;s Cruz

Reputation: 14970

It all seems correct, except for the operator used on the $start parameter. You have

->where('end_auction', '=>', $start)

And you should have

->where('end_auction', '>=', $start)

Notice the difference between => and >=. The first throws a MySQL error. You could try to wrap that code around a try ... catch block, and check the exception message.

You can also log the executed queries using one of this answers, to check the executed query whenever you don't get the expected results.

Upvotes: 1

Related Questions