Laravel 查询生成器之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41131416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Query Builder wherebetween
提问by Egzan
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 ?
有什么帮助吗?
回答by Zakaria Acharki
You've to pass it to the function using use
like :
您必须使用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.
希望这可以帮助。