简体   繁体   English

Laravel Query Builder介于两者之间

[英]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. 未定义的变量:$ datefrom等

Any help ? 有帮助吗?

You've to pass it to the function using use like : 你已经将它传递给使用该功能use ,如:

->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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM