Laravel Query Builder wherebetween
Asked Answered
R

1

9

I build a search form which will select markers from database where date is inserted in datapicker. That works fine but , when I add time I get error .

I rebuilded a query to something like that :

$datefrom = $request->input('datefrom');
$dateto = $request->input('dateto');
$timefrom = $request->input('timefrom');
$timfrom = $timefrom.':00';
$timeto = $request -> input('timeto');
$timto = $timeto.':00';
$type = $request->input('type');

$maps = Map::select('lat','lng',$type,'temp','humidity','date','time')
                ->where(function($query){
                    $query->whereBetween('date',array($datefrom,$dateto))
                    ->whereBetween('time',array($timfrom,$timto));
                })
                ->get();

When I enter check on search form I get error :

Undefined variable : $datefrom etc.

Any help ?

Runyan answered 13/12, 2016 at 22:10 Comment(0)
I
14

You've to pass it to the function using use like :

->where(function($query) use ($datefrom){
     $query->whereBetween('date',array($datefrom,$dateto))
           ->whereBetween('time',array($timfrom,$timto));
})

If you've more than one you could pass them like :

->where(function($query) use ($datefrom,$dateto,$timfrom,$timto){
     $query->whereBetween('date',array($datefrom,$dateto))
           ->whereBetween('time',array($timfrom,$timto));
})

Hope this helps.

Isonomy answered 13/12, 2016 at 22:12 Comment(1)
That works but , timfrom must be lower then timto even dateto is bigger then datefrom. How to change it that it will works for example if we get : from 2016-04-04 15:45 to 2016-04-08 12:12Runyan

© 2022 - 2024 — McMap. All rights reserved.