Laravel多数据查询
db包含一个带有dateSTART和dateSTOP字段的用户列表。 通过查询,我应该获得与当前月份的特定用户相关的记录,即使两个日期字段(开始或停止)中只有一个包含当前月份,这也是有效的。 考虑到起始值和停止值可以在当前月份附近,查询应考虑OR。 我尝试如下所述,但函数中出现了一个错误,与作为$request传递的lastname和name参数有关Laravel多数据查询,laravel,Laravel,db包含一个带有dateSTART和dateSTOP字段的用户列表。 通过查询,我应该获得与当前月份的特定用户相关的记录,即使两个日期字段(开始或停止)中只有一个包含当前月份,这也是有效的。 考虑到起始值和停止值可以在当前月份附近,查询应考虑OR。 我尝试如下所述,但函数中出现了一个错误,与作为$request传递的lastname和name参数有关 public function listOfMonth(Request $request){ $lastname=$request->
public function listOfMonth(Request $request){
$lastname=$request->get('lastname');
$name=$request->get('name');
$result=DB::table('timbraturas')
->where('lastname',$lastname)
->where('name',$name)
->whereMonth('dateSTART','=',Carbon::now()->month)
->whereYear('dateSTART','=',Carbon::now()->year)
->orWhere(function($query) {
$query->where('lastname',$lastname)
->where('name',$name)
->whereYear('dateSTOP','=',Carbon::now()->year)
->whereMonth('dataSTOP','=',Carbon::now()->month);
})
->orderBy('id','asc')
->get();
return $result;
}
调用这样的嵌套函数时,如果变量是在外部作用域中用
定义的,则需要传入变量。使用如下所示:
function($query) use ($name, $lastname) {
如果需要将变量传递给Clousure,则必须使用use
关键字,如下所示:
function($query) use ($name, $lastname) {
公共功能月列表(请求$Request){
$lastname=$request->get('lastname');
$name=$request->get('name');
$result=DB::table('timbraturas')
->其中('lastname',$lastname)
->其中('name',$name)
->whereMonth('dateSTART','=',Carbon::now()->month)
->whereYear('dateSTART','=',Carbon::now()->year)
->orWhere(函数($query)use($lastname,$name){
$query->where('lastname',$lastname)
->其中('name',$name)
->whereYear('dateSTOP','=',Carbon::now()->year)
->其中月份('dataSTOP','=',Carbon::now()->month);
})
->订购人('id','asc')
->get();
返回$result;
}
错误是什么?太好了!它起作用了!