infernobass7's avatar

Check if a model uses Soft Deleting

I am trying to find out if there is a way to find out if a model is soft deleting from the view.

1 like
8 replies
ctf0's avatar

based on @nate.a.johnson

in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))
9 likes
gregrobson's avatar

Use @ctf0's code in an accessor attribute:

// In your model...
public function getSoftDeletingAttribute()
{
    // ... check if 'this' model uses the soft deletes trait
    return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this)) && ! $this->forceDeleting;
}

$myModel->soft_deleting; // True if the SoftDeletes trait is in use AND forceDeleting is off.
3 likes
renky's avatar

Or just check for a method of the SoftDelete-Trait:

method_exists($this, 'bootSoftDeletes')
2 likes

Please or to participate in this conversation.