Laravel wherebetween with orwherebetween

Laravel wherebetween with orwherebetween

我写了一个 Laravel 查询。我在名为 'shareable' 的列上使用 whereBetweenorWhereBetween 以及 where 语句,应该 return 一行。
但是这个查询没有考虑 where 条件。只用了 whereBetweenorWhereBetween。可能是什么原因。

我的查询

$childs = Program::whereIn('id', $programs_by_date)
                ->where('shareable', '=','1')
                ->wherebetween('starting_date', [$new_start_date,$new_end_date])
                ->orwherebetween('ending_date', [$new_start_date,$new_end_date])
                ->orderBy('starting_date')
                ->get(); 

->where('shareable', '=','1') return 0 行。可能是什么原因。

shareable 类型为枚举

您应该将 whereBetween & orWhereBetween 包裹在 where 中。试试这个:

$childs = Program::whereIn('id', $programs_by_date)
            ->where('shareable', '=','1')
            ->where(function($query) use ($new_start_date, $new_end_date){
                  $query->whereBetween('starting_date', [$new_start_date,$new_end_date])
                        ->orWhereBetween('ending_date', [$new_start_date,$new_end_date]);
                })
            ->orderBy('starting_date')
            ->get();

希望这会奏效。

你应该试试这个

$child = Program::whereIn('id', $programs_by_date)
                ->where('shareable', '=','1')
                ->whereRaw("( starting_date BETWEEN '".$new_start_date."' AND '".$new_end_date."' OR ending_date BETWEEN '".$new_start_date."' AND '".$new_end_date."' ")
                ->orderBy('starting_date')
                ->get();