Laravel 4.2如何确定模型是否为软删除或强制删除

时间:2015-02-02 08:52:52

标签: laravel cascading-deletes soft-delete

这是我在发票模型中的代码:

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()是软删除还是强制删除?

1 个答案:

答案 0 :(得分:3)

模型具有属性forceDeleting。哪个是truefalse

if($invoice->forceDeleting){
    //do in case of force delete
}
else {
    //do in case of soft delete
}

注意,此属性仅在使用SoftDeletingTrait时可用。所以要小心在某种基本模型中使用它。