我正在阅读laravel softdelete并在此恢复级联:Laravel 5: cascade soft delete
用户说:
You should use Eloquent events for this.
Offers::deleted(function($offer) {
$offer->services()->delete();
});
Offers::restored(function($offer) {
$offer->services()->withTrashed()->restore();
});
他没有提到放置此代码的位置,我有兴趣收听雄辩删除和恢复的事件。我在哪里可以放这个代码?我可以在模型课中听吗?如果不在哪里放置它?
答案 0 :(得分:1)
我想......
<?php
class Attribute extends Model implements Transformable
{
//....
protected static function boot() {
parent::boot();
static::deleting(function($model) {
foreach ($model->attributeValue()->get() as $attributeValue) {
$attributeValue->delete();
}
});
}
或者示例:
class BaseModel extends Model
{
public static function boot()
{
static::creating(function ($model) {
// blah blah
});
static::updating(function ($model) {
// bleh bleh
});
static::deleting(function ($model) {
// bluh bluh
});
parent::boot();
}
}