Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 基于雄辩查询的参数组_Laravel - Fatal编程技术网

Laravel 基于雄辩查询的参数组

Laravel 基于雄辩查询的参数组,laravel,Laravel,目前我的搜索查询没有什么问题。当我只使用参数comite_id运行搜索查询时,它正在工作,但当我想使用comite_id和WhereBeween以及date_min和date_max运行搜索查询时,结果是不正确的,因为我没有得到正确的日期范围,结果似乎与WhereBeween子句无关 有人知道我哪里做错了?提前多谢 这里是我的控制器: public function getRetrocession(Request $request){ $comites = Stru

目前我的搜索查询没有什么问题。当我只使用参数comite_id运行搜索查询时,它正在工作,但当我想使用comite_id和WhereBeween以及date_min和date_max运行搜索查询时,结果是不正确的,因为我没有得到正确的日期范围,结果似乎与WhereBeween子句无关

有人知道我哪里做错了?提前多谢

这里是我的控制器:

  public function getRetrocession(Request $request){

            $comites = Structure::where('type_structure_id' , '=' ,'3')->pluck('nom_structure' , 'id');

            $search = $request->input('comite_id');
            $dt_min = $request->input('dt_min');
            $dt_max =  $request->input('dt_max');


            if ($search) {
                $query = Retrocession::where('comite_id', '=', $search)

                        ->orWhere(function ($q) use($search , $dt_min , $dt_max) {
                            $q->where('comite_id', '=', $search)
                                ->whereBetween('dt_retrocession', [ $dt_min , $dt_max]);
                        });

            }else {
                $query = Retrocession::select();
            }

            $retrocessions = $query->orderBy('dt_retrocession', 'DESC')->paginate(10)
                ->appends(['recherche' => $search]);

            return view('retrocession/index' , compact('retrocessions' , 'comites'));


        }
因为你在使用orWhere,它总是同时调用where和orWhere;因此包括where和orWhere的结果。你可以这样试试

$query = Retrocession::where('comite_id', $search)
->when($dt_min && $dt_max, function($q) use ($dt_min, $dt_max) {
    $q->whereBetween('dt_retrocession', [$dt_min, $dt_max]);
});
所以您总是运行第一个where,并且仅当$dt_min和$dt_max都为真时才包括where

博恩·钱斯·马修