Skip to content

Commit

Permalink
add forceDeleted event
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 26, 2018
1 parent c9e6100 commit 497a907
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function getObservableEvents()
{
return array_merge(
[
'retrieved', 'creating', 'created', 'updating',
'updated', 'deleting', 'deleted', 'saving',
'saved', 'restoring', 'restored',
'retrieved', 'creating', 'created', 'updating', 'updated',
'saving', 'saved', 'restoring', 'restored',
'deleting', 'deleted', 'forceDeleted',
],
$this->observables
);
Expand Down
10 changes: 6 additions & 4 deletions src/Illuminate/Database/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public function forceDelete()
{
$this->forceDeleting = true;

$deleted = $this->delete();
return tap($this->delete(), function ($deleted) {
$this->forceDeleting = false;

$this->forceDeleting = false;

return $deleted;
if ($deleted) {
$this->fireModelEvent('forceDeleted', false);
}
});
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Integration/Database/EloquentDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* @group integration
Expand Down Expand Up @@ -40,6 +41,12 @@ public function setUp()
$table->integer('post_id');
$table->timestamps();
});

Schema::create('roles', function ($table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
});
}

public function testOnlyDeleteWhatGiven()
Expand All @@ -56,6 +63,20 @@ public function testOnlyDeleteWhatGiven()
Post::join('comments', 'comments.post_id', '=', 'posts.id')->where('posts.id', '>', 1)->orderBy('posts.id')->limit(1)->delete();
$this->assertEquals(8, Post::all()->count());
}

public function testForceDeletedEventIsFired()
{
$role = Role::create([]);
$this->assertInstanceOf(Role::class, $role);
Role::observe(new RoleObserver);

$role->delete();
$this->assertNull(RoleObserver::$model);

$role->forceDelete();

$this->assertEquals($role->id, RoleObserver::$model->id);
}
}

class Post extends Model
Expand All @@ -68,3 +89,20 @@ class Comment extends Model
public $table = 'comments';
protected $fillable = ['post_id'];
}

class Role extends Model
{
use SoftDeletes;
public $table = 'roles';
protected $guarded = [];
}

class RoleObserver
{
public static $model;

public function forceDeleted($model)
{
static::$model = $model;
}
}

0 comments on commit 497a907

Please sign in to comment.