Skip to content

Commit 45829d0

Browse files
Merge pull request #6 from michaeldyrynda/enhance-usability
Implement the ability to inherit cascading deletes
2 parents dff2dd4 + 06f78ad commit 45829d0

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/CascadeSoftDeletes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected static function bootCascadeSoftDeletes()
4747
*/
4848
protected function implementsSoftDeletes()
4949
{
50-
return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this));
50+
return method_exists($this, 'runSoftDelete');
5151
}
5252

5353

tests/CascadeSoftDeletesIntegrationTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ public function it_handles_situations_where_the_relationship_method_does_not_exi
137137
$post->delete();
138138
}
139139

140+
/** @test */
141+
public function it_handles_soft_deletes_inherited_from_a_parent_model()
142+
{
143+
$post = Tests\Entities\ChildPost::create([
144+
'title' => 'Testing child model inheriting model trait',
145+
'body' => 'This should allow a child class to inherit the soft deletes trait',
146+
]);
147+
148+
$this->attachCommentsToPost($post);
149+
150+
$post->delete();
151+
152+
$this->assertNull(Tests\Entities\ChildPost::find($post->id));
153+
$this->assertCount(1, Tests\Entities\ChildPost::withTrashed()->where('id', $post->id)->get());
154+
$this->assertCount(0, Tests\Entities\Comment::where('post_id', $post->id)->get());
155+
}
156+
140157
/**
141158
* Attach some dummy comments to the given post.
142159
*

tests/Entities/ChildPost.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tests\Entities;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Tests\Entities\Post;
7+
use Tests\Entities\Comment;
8+
9+
class ChildPost extends Post
10+
{
11+
protected $table = 'posts';
12+
13+
public function comments()
14+
{
15+
return $this->hasMany('Tests\Entities\Comment', 'post_id');
16+
}
17+
}

0 commit comments

Comments
 (0)