2016-04-15 123 views
0

所以我有一個數據透視表thread_user,我有兩個模型; User.php和Thread.php。Laravel - belongsToMany關係時間戳

我有這個在我的user.php的文件:

public function threads() 
    { 
     return $this->belongsToMany('App\Thread')->withTimestamps(); 
    } 

我只希望created_at時間戳雖然。我不想要updated_at,並且每次嘗試將某些內容附加到數據透視表時,我都會收到此錯誤:Column not found: 1054 Unknown column 'updated_at' in 'field list'...。這真的很煩人。由於某些原因,我不能有updated_at時間戳,但我仍然需要created_at時間戳。

請讓我知道如何解決這個問題。我已經試過,沒有運氣以下:

return $this->belongsToMany('App\Thread')->withTimestamps(['created_at']);

請幫助。謝謝!

回答

0

由於您沒有使用兩個時間戳,因此您需要刪除對withTimestamps()的呼叫,並且只需調用withPivot()即可獲得您所擁有的字段。

所以,你的代碼應該是這樣的:

public function threads() 
{ 
    return $this->belongsToMany('App\Thread')->withPivot('created_at'); 
}