Laravel 非法运算符和值组合异常 with() 与belongsTo() 关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35454512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:15:10  来源:igfitidea点击:

Laravel Illegal operator and value combination Exception on with() with belongsTo() relation

phplaravellaravel-5.2

提问by Danish Altaf Satti

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

我有一个带有belongsTo()关系的调用模型如下

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

    }
}

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

这种关系的逻辑是,如果两个表中的 gsm_numbers 匹配,并且 Campaign 中的 call_start from start_date 未设置或小于 Call 模型中的 call_start 且 Campaign 模型中的 end_date 未设置或大于,则每个调用都属于一个活动比调用模型中的 call_start

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

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

没问题,但我还需要对 ajax 调用执行相同的问题,因此我需要调用数据和活动。所以我做以下

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

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

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

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

Exception Stack:

异常堆栈:

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

回答by Ayo Akinyemi

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

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

 $call->campaign['name']

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

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

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

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

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.

查询构建器调用您的关系“campaign”,并将您的结果评估$callStart = $this->call_start;为 null,因为 Call 还不是有效的模型实例,还没有数据设置到它,因此 call_start 属性此时将为 null。