问题
I have written a Laravel query. I am using whereBetween
and orWhereBetween
along with a where
statement on a column named 'shareable', which should return one row.
But this query does not take where
condition into consideration. It took only whereBetween
and orWhereBetween
. What can be the reason.
my query
$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 rows. What can be the reason.
shareable
type is enum
回答1:
Try this:
$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();
Hope this will work.
回答2:
You Should try this
$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();
来源:https://stackoverflow.com/questions/49875063/laravel-wherebetween-with-orwherebetween