Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Query Builder wherebetween

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 ?

like image 477
Egzan Avatar asked Dec 13 '16 22:12

Egzan


1 Answers

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.

like image 87
Zakaria Acharki Avatar answered Oct 17 '22 07:10

Zakaria Acharki