因此,我有一个名为thread_user的数据透视表,并且有两个模型。 User.php和Thread.php。

我的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');
}

关于php - Laravel-属于多个关系的时间戳,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36637196/

10-14 13:04
查看更多