Search Orange/Eloquent dates interval?

Asked

Viewed 927 times

2

How to search the date range?

I searched and many places indicate how to search for date ranges as example below, but without result.

$d1 = data2Mysql($dataI); // format Y-m-d
$d2 = data2Mysql($dataF); // format Y-m-d

$current = DB::table('ponto')
        ->whereBetween('data_inicial',[$d1,$d2])
        ->first();

The Eloquent correctly mounts SQL, but the result is null and there are data in the table where the range is being searched.

Executed query:

array(1) {
  [0]=>
  array(3) {
    ["query"]=>
    string(66) "select * from `ponto` where `data_inicial` between ? and ? limit 1"
    ["bindings"]=>
    array(2) {
      [0]=>
      string(10) "2018-01-12"
      [1]=>
      string(10) "2018-01-13"
    }
    ["time"]=>
    float(0.95)
  }
}
NULL

1 answer

4


A teammate passed me an example in another language and using a whereRaw of eloquent achieved the expected result and I’m sharing:

   $d1 = data2Mysql($dataI); // format Y-m-d

    $current = DB::table('ponto')
        ->whereRaw('? between data_inicial and data_final', $d1)
        ->first();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.