繁体   English   中英

Laravel-属于多个关系的时间戳

[英]Laravel - belongsToMany relationship timestamp

因此,我有一个名为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');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM