【问题标题】:Timestamps are not updating while attaching data in pivot table在数据透视表中附加数据时时间戳不更新
【发布时间】:2014-11-01 19:27:58
【问题描述】:

我正在使用以下附加语句在数据透视表中创建一行。

$music = Music::find(1);
$music->users()->attach(1);

这会在数据透视表中插入一行,但它不会更新时间戳。时间戳仍然是 0000-00-00 00:00:00

有什么方法可以更新数据透视表中的时间戳。 ?

提前致谢,

【问题讨论】:

    标签: laravel laravel-4 eloquent


    【解决方案1】:

    如果您希望您的数据透视表自动维护 created_at 和 updated_at 时间戳,使用withTimestamps() 关系定义的方法。

    return $this->belongsToMany('Role')->withTimestamps();
    

    【讨论】:

    • 这必须在关系的两边吗?还是只有一侧?
    • @Noitidart 我刚刚检查过,看来它需要在关系的双方才能工作。
    猜你喜欢
    • 2019-03-15
    • 1970-01-01
    • 2021-04-23
    • 2013-10-05
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    相关资源
    最近更新 更多