laravel 在数据透视表中附加数据时时间戳不更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25726602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:43:25  来源:igfitidea点击:

Timestamps are not updating while attaching data in pivot table

laravellaravel-4eloquent

提问by Sriraman

I'm creating a row in pivot table using the following attach statement.

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

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

This inserting a row in pivot table but, It is not updating the timestamp. The timestamp remains 0000-00-00 00:00:00

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

Is there any way to update the timestamps in pivot table. ?

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

Thanks in advance,

提前致谢,

回答by marcanuy

If you want your pivot table to have automatically maintained created_at and updated_at timestamps, use the withTimestamps()method on the relationship definition.

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

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