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
Timestamps are not updating while attaching data in pivot table
提问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();
- For Laravel 4.2: Working With Pivot Tables
- Laravel 5.0: Working With Pivot Tables
- Laravel 5.1: Retrieving Intermediate Table Columns
- Laravel 5.2: Filtering Relationships Via Intermediate Table Columns
- Laravel 5.3: Filtering Relationships Via Intermediate Table Columns
- Laravel 5.4: Retrieving Intermediate Table Columns
- Laravel 5.5: Retrieving Intermediate Table Columns
- Laravel 5.6: Retrieving Intermediate Table Columns
- Laravel 5.7: Retrieving Intermediate Table Columns
- Laravel 5.8: Retrieving Intermediate Table Columns
- 对于 Laravel 4.2:使用数据透视表
- Laravel 5.0:使用数据透视表
- Laravel 5.1:检索中间表列
- Laravel 5.2:通过中间表列过滤关系
- Laravel 5.3:通过中间表列过滤关系
- Laravel 5.4:检索中间表列
- Laravel 5.5:检索中间表列
- Laravel 5.6:检索中间表列
- Laravel 5.7:检索中间表列
- Laravel 5.8:检索中间表列