laravel query: orWhere: double condition

倖福魔咒の 提交于 2019-12-25 07:58:30

问题


I have the following query, which does not give me the expected result:

    $query = $query->join('events_dates', function($join) use ($data){ 
                $join->on('events.id', '=', 'events_dates.event_id')
                        ->where('events_dates.start_date', "<=", date_format(date_create($data['date_end']), "Y-m-d"))
                        ->where('events_dates.end_date', '>=', date_format(date_create($data['date_start']), "Y-m-d"))
                        ->orWhere('recurrent', "=", 1)
                        ->where((strtotime($data["date_start"]) - strtotime('event_dates.start_date')) % ('events_dates.repeat_interval' * 86400), '=', 0); 
            }); 

There are 4 where clauses in this query. The requirement is that either the two first where clauses are executed, or either two last depending on the recurrentfield.

PHP returns an error division by zero, because the last Where clause should not be executed when recurrentis 0.

Any suggestions?


回答1:


I don't know your exact goal nor do I know what your db looks so this is just a wild guess:

$query = $query->join('events_dates', function($join) use ($data){ 
            $join->on('events.id', '=', 'events_dates.event_id')
                    ->where('events_dates.start_date', "<=", date_format(date_create($data['date_end']), "Y-m-d"))
                    ->where('events_dates.end_date', '>=', date_format(date_create($data['date_start']), "Y-m-d"))
                    ->orWhere('recurrent', "=", 1)
                    ->whereRaw('DATEDIFF(?, event_dates.start_date) % event_dates.repeat_interval = 0', array($data['date_start']));

Update

This might help for the between two dates part

->where('events_dates.start_date', '<=', new Carbon($data['date_end']))
->where('events_dates.end_date', '>=', new Carbon($data['date_start']))


来源:https://stackoverflow.com/questions/27707974/laravel-query-orwhere-double-condition

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