在 Laravel 5.1 中,我有两个模型。城市模型和照片模型。
City 和 Photo 之间存在多态关系。
更新城市照片时
$city->photos()->updateOrCreate($attributes,$values)
子时间戳更新。但父模型的时间戳(在本例中为 City)不会相应更新,我应该手动调用
$city->touch()
在 Laravel 中触摸子模型时如何更新父模型的时间戳?
最佳答案
对于多态关系
class Photo extends Eloquent {
protected $touches = ['city'];
public function city() {
return $this->morphTo() // add this function if not already done
}
}
class City extends Eloquent {
public function photos() {
return $this->morphMany(App\Photo::class, 'city');
}
}
在这种情况下,当照片更新时,它会触及他的 parent (在这种情况下是城市)。
希望有帮助。
关于laravel - 在多态关系中更新子模型时更新父时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32300323/