Laravel - belongsToMany关系时间戳
问题描述:
所以我有一个数据透视表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']);
请帮助。谢谢!
答
由于您没有使用两个时间戳,因此您需要删除对withTimestamps()
的呼叫,并且只需调用withPivot()
即可获得您所拥有的字段。
所以,你的代码应该是这样的:
public function threads()
{
return $this->belongsToMany('App\Thread')->withPivot('created_at');
}