2017-08-02 43 views
1

我有时间戳字段,命名为关闭,在枢纽表中为多对多关系。我想获得项目,其中已关闭为空或小于特定时间。 我试过如下:Laravel雄辩在哪里枢轴null或小于

// In model 
public function jobEquipments($job, $one = false) 
    { 
     $nowDt = Carbon::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s')); 
     if (!$one){ 
     return $this->belongsToMany(Equipment::class, 'cavity_actions', 'cavity_id', 'equipment_id') 
       ->wherePivot('job_id', $job) 
       ->withPivot('created_at','aid') 
       ->wherePivot('closed',null) 
       ->orWherePivot('closed','<',date('Y-m-d H:i:s')) 
       ->orderBy('pivot_created_at', 'desc'); 
      } 
.... 

我还试图用$nowDt,而不是date('Y-m-d H:i:s')orWherePivot然而,查询结果没有变化。即它看起来像没有->orWherePivot('closed','<',date('Y-m-d H:i:s'))子句的相同值。在我的数据库中,我非常肯定,有足够的记录关闭日期时间值小于Now

回答

2

尝试隔离or语句。试试这个:

$this->belongsToMany(Equipment::class, 'cavity_actions', 'cavity_id', 'equipment_id') 
       ->wherePivot('job_id', $job) 
       ->withPivot('created_at','aid') 
       ->where(function ($q) { 
        $q->where('cavity_actions.closed',null) 
         ->orWhere('cavity_actions.closed','<',date('Y-m-d H:i:s')); 
       }) 
       ->orderBy('pivot_created_at', 'desc'); 
+0

'调用未定义的方法照亮\数据库\查询\生成器:: orWherePivot()' – SaidbakR

+0

好,尽量用在哪里,orWhere。我编辑了答案 – Laerte

+0

最后的编辑工作,但它返回的结果与我认为的代码没有隔离相同。 – SaidbakR