【发布时间】:2019-09-11 06:06:31
【问题描述】:
我设置了一个模型事件来检查图像何时被删除并删除相关的 image_size 模型条目。但是图像使用软删除,所以如果它被软删除,那么我想软删除 image_size 记录,但如果使用 forceDelete 硬删除图像,那么我想硬删除 image_size 记录。有没有办法检测它是什么类型的删除并采取相应的行动。以下是我的 Image 模型中的内容:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Image extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['url', 'thumb_url', 'filename'];
/**
* Relationship to image sizes
*/
public function sizes()
{
return $this->hasMany('App\Image_size');
}
/**
* Model events
*/
protected static function boot() {
parent::boot();
static::deleting(function($image) { // before delete() method call this
$image->sizes()->delete();
});
}
}
【问题讨论】: