2017-08-02 60 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