Laravel wherebetween with orwherebetween

感情迁移 提交于 2020-01-14 05:43:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!