【发布时间】:2015-08-30 19:29:04
【问题描述】:
在 Laravel 5.1 中,我有两个模型。一个城市模型和一个照片模型。
City 和 Photo 之间存在多态关系。
使用
$city->photos()->updateOrCreate($attributes,$values)
子时间戳更新。但是父模型的时间戳,在这种情况下是 City,没有相应更新,我应该手动调用
$city->touch()
在 Laravel 中触摸子模型时如何更新父模型的时间戳?
【问题讨论】:
-
如果您希望数据透视表自动维护 created_at 和 updated_at 时间戳,请在关系定义上使用 withTimestamps 方法: return $this->belongsToMany('App\Role')->withTimestamps() ;
-
我很想知道您喜欢这种行为的原因?谢谢
标签: laravel polymorphism eloquent laravel-5.1