Laravel非法操作符和值组合Exception with with()具有归属关系()关系 [英] Laravel Illegal operator and value combination Exception on with() with belongsTo() relation

查看:404
本文介绍了Laravel非法操作符和值组合Exception with with()具有归属关系()关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下调用模型,该模型具有一个belongsTo()关系

I have a Call Model with a belongsTo() relation as following

class Call extends Model
{
    public function campaign()
    {
        $callStart = $this->call_start;
        return $this->belongsTo('App\Campaign','gsm_number','gsm_number')
        ->where(function($query) use($callStart){
            $query->where('end_date','=','0000-00-00 00:00:00')
                ->orWhere('end_date','>=',$callStart);
        })->where(function($query) use($callStart){
            $query->where('start_date','=','0000-00-00 00:00:00')
                ->orWhere('start_date','<=',$callStart);
        });

    }
}

这种关系的逻辑是,如果两个表中的gsm_numbers匹配并且未设置Campaign中从start_date的call_start或未设置其小于调用模型中的call_start且没有设置Campaign模型中的end_date,则每个呼叫都属于活动.它大于呼叫模型中的call_start

The logic of this relation is that, each call belongs to a campaign if gsm_numbers in both tables match and call_start from start_date in Campaign is either not set or less than call_start in Call model and end_date in Campaign model is wither not set or it is greater than call_start from Call Model

在控制器中,我这样做:

In Controller I do:

$calls = Call::orderBy('call_start','DESC')->get(); //in simplest form
return view('calls')->with('calls',$calls);

在查看广告系列列表"中,我还使用以下方法显示广告系列信息

In View for campaigns listing I display campaign information as well using following

@foreach($calls as $call)
      {{ $call->campaign['name'] }}
@endforeach

没问题,但是我也需要对ajax调用执行相同的问题,因此我需要与广告系列一起使用的调用数据.所以我要做以下

No issue but I need to perform same issue with ajax calls as well so I need calls data along with campaigns. So I do the following

$calls = Call::with('campaign')->orderBy('call_start','DESC')
            ->get();

if($request->ajax()){
  return $calls
}

在这种情况下,我得到异常InvalidArgumentException in Builder.php line 464: Illegal operator and value combination.

In this case I get exception InvalidArgumentException in Builder.php line 464: Illegal operator and value combination.

异常堆栈:

in Builder.php line 464
at Builder->where('end_date', '>=', null, 'or')
at call_user_func_array(array(object(Builder), 'where'), array('end_date', '>=', null, 'or')) in Builder.php line 640
at Builder->where('end_date', '>=', null, 'or') in Builder.php line 656
at Builder->orWhere('end_date', '>=', null) in Call.php line 51
at Call->App\{closure}(object(Builder))

推荐答案

您不能对日期时间字段执行空比较.您需要确保$ callStart不为null,然后再添加where子句->orWhere('end_date','>=',$callStart);

You cannot perform a null comparison on a datetime field. You need to make sure $callStart is not null before adding a where clause ->orWhere('end_date','>=',$callStart);

 $call->campaign['name']

有效,因为$ call存在.也就是说,它包含数据.因此$callStart = $this->call_start;不会为null.但是当你有

works because $call exists. that is, it contains data. hence $callStart = $this->call_start; will not be null. but when you have

$calls = Call::with('campaign')->orderBy('call_start','DESC')->get();

查询构建器将您的关系称为广告系列",并将您的$callStart = $this->call_start;评估为null,因为Call仍不是有效的模型实例,因此尚未为其设置任何数据,因此call_start属性在那时将为null

the query builder calls your relation 'campaign', and evaluates your $callStart = $this->call_start; as null, since Call is not yet a valid model instance, no data is set to to it yet, hence call_start attribute will be null at that point.

这篇关于Laravel非法操作符和值组合Exception with with()具有归属关系()关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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