7

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

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?

Chris
  • 4,277
  • 7
  • 40
  • 55

6 Answers6

12

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
}
RMcLeod
  • 2,561
  • 1
  • 22
  • 38
  • 1
    Thanks, you actually answered that at the same time I was writing my own answer. I'll mark yours as the answer since it's basically the same as mine. But I found when I used class_uses(), it returned an array with the entire namespace, not just SoftDeletingTrait, so that's what I had to look for: 'Illuminate\Database\Eloquent\SoftDeletingTrait' – Chris Aug 05 '14 at 14:29
3

I needed to detect soft deletion on a model where the trait had been included in a parent class, so class_uses() did not work for me. Instead, I checked for the bootSoftDeletingTrait() method. Something along the lines of:

// Class Name $usesSoftDeletes = method_exists('User', 'bootSoftDeletingTrait');

or

// Model Instance $usesSoftDeletes = method_exists($model, 'bootSoftDeletingTrait');

should work.

2

For Laravel 5.x only

If checking on main model:

//class_uses retrieves a list of traits from the object passed into an array
//Hence in_array check for name of trait (in string format)
//@param bool $usesSoftDeletes

$usesSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model));

If you are checking on the main model's relationship, use the following:

//Replace `myRelationshipName` by the name of the relationship you are checking on.
//getRelated() function fetches the class of the relationship.
//@param bool $relatedUsesSoftDeletes

$relatedModel = $model->myRelationshipName()->getRelated();
$relatedUsesSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($relatedModel));
Mysteryos
  • 5,581
  • 2
  • 32
  • 52
0

There's basically not a direct approach on knowing if a model soft deletes by calling a function since the method isSoftDeleting() has been removed from 4.2. You know if a model is using soft delete if the use SoftDeletingTrait; is present in your Model's class and when the deleted_at column exists in your database table.

You can basically trust Laravel on removing a record from your database using soft delete when you have defined the use SoftDeletingTrait in your model's class, and have the deleted_at (is a TIMESTAMP) in your model's database table.

Robin
  • 132
  • 7
  • Thanks. I know I can check the database for that "deleted_at" column, but I was hoping to avoid that. My answer provides another solution. – Chris Aug 05 '14 at 14:27
0

Well, I figured out out a good enough solution for my needs.

First I make this call:

$traits = class_uses($model);

Then I check for the the softdeletingtrait

$usesSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletingTrait', $traits);

At least this way I avoid having to call the database for each model I test. Though it'll break if they change the SoftDeletingTrait name or location later on...

Chris
  • 4,277
  • 7
  • 40
  • 55
-1

This is the best way

$model = 'App\\Models\\ModelName';

$uses_soft_delete = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model));

if($uses_soft_delete) {
    // write code...
}