这是我在发票模型中的代码:
class Invoice extends Eloquent {
protected $table = "invoice";
protected $guarded = ["id"];
protected $deleted_at = ['deleted_at'];
use SoftDeletingTrait;
public static function boot() {
parent::boot();
Invoice::deleted(function($invoice) {
if($invoice->isSoftDelete()) {
//do in case of soft delete
} else if($invoice->isForceDelete()) {
//do in case of force delete
}
});
}
}
例如:
$invoice = Invoice::find(1);
删除$ invoice(可能是软删除或强制删除)
$invoice->delete();
$invoice->forceDelete();
如何确定$ invoice上面的函数boot()是软删除还是强制删除?
答案 0 :(得分:3)
模型具有属性forceDeleting
。哪个是true
或false
:
if($invoice->forceDeleting){
//do in case of force delete
}
else {
//do in case of soft delete
}
注意,此属性仅在使用SoftDeletingTrait
时可用。所以要小心在某种基本模型中使用它。