laravelmodeleloquentsoft-delete

How to determine if a model uses soft deletes in Laravel 4.2


How do I determine if a model uses soft deletes in Laravel?

In the Laravel API I found the function isSoftDeleting(), but apparently that was removed from Laravel 4.2 now that it uses the SoftDeletingTrait.

How would I go about determining if a model uses soft deletes now?


Solution

  • If you want to check programatically whether a Model uses soft deletes you can use the PHP function class_uses to determine if your model uses the SoftDeletingTrait

    // You can use a string of the class name
    $traits = class_uses('Model');
    // Or you can pass an instance
    $traits = class_uses($instanceOfModel);
    
    if (in_array('SoftDeletingTrait', $traits))
    {
        // Model uses soft deletes
    }
    
    // You could inline this a bit
    if (in_array('SoftDeletingTrait', class_uses('Model')))
    {
        // Model uses soft deletes
    }