Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel/Eloquent 5 whereBetween not working as expected

I am using Laravel 5, and I would like get rows between two dates in my database (SQLite).

I am using this command:

$trackers = Tracker::whereBetween('detect_at', [$date1, $date2])->where("serial", "uhu")->get();

But I get no response.

Entire request:

$date1 = "2015-07-13 03:53:38";
$date2 = "2015-07-13 03:58:36";
$trackers = Tracker::whereBetween('detect_at', [$date1, $date2])->where("serial", "uhu")->get();

Here is an example of one row in my database:

id   serial  latitude   longitude   altitude    accuracy    detect_at                     created_at               updated_at
728  uhu    48.0491     -1.74138    0           20          2015-07-13 03:54:38           2015-07-02 13:40:15         2015-07-02 13:40:15
like image 513
Hydral Avatar asked Aug 06 '15 19:08

Hydral


1 Answers

Wrap the dates in Carbon objects:

// At top of file
use Carbon\Carbon;

$trackers = Tracker
    ::whereBetween('detect_at', [new Carbon($date1), new Carbon($date2)])
    ->where("serial", "uhu")
    ->get();
like image 126
Ben Claar Avatar answered Oct 17 '22 23:10

Ben Claar