laravel查询:或其中:双重条件 [英] laravel query: orWhere: double condition

查看:146
本文介绍了laravel查询:或其中:双重条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询,但没有得到预期的结果:

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); 
            }); 

此查询中有4个where子句. 要求是根据recurrent字段执行前两个where子句,或最后两个执行.

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返回错误division by zero,因为当recurrent0时不应执行最后的Where子句.

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

有什么建议吗?

推荐答案

我不知道您的确切目标,也不知道您的数据库的外观,所以这只是一个疯狂的猜测:

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']))

这篇关于laravel查询:或其中:双重条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆