diff --git a/src/TurboStreamResponseMacro.php b/src/TurboStreamResponseMacro.php index 6d77d9df..b0866e90 100644 --- a/src/TurboStreamResponseMacro.php +++ b/src/TurboStreamResponseMacro.php @@ -16,7 +16,9 @@ public function __construct(TurboStreamModelRenderer $renderer) public function handle(Model $model, string $action = null) { - if (! $model->exists) { + if (! $model->exists || + (method_exists($model, 'trashed') && $model->trashed()) + ) { return $this->renderModelDeletedStream($model); } diff --git a/tests/Http/ResponseMacrosTest.php b/tests/Http/ResponseMacrosTest.php index 09a7b33c..81c64c9b 100644 --- a/tests/Http/ResponseMacrosTest.php +++ b/tests/Http/ResponseMacrosTest.php @@ -2,6 +2,7 @@ namespace Tonysm\TurboLaravel\Tests\Http; +use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\View; use Tonysm\TurboLaravel\Models\Broadcasts; use Tonysm\TurboLaravel\Tests\TestCase; @@ -111,6 +112,21 @@ public function streams_model_on_delete() $this->assertEquals(Turbo::TURBO_STREAM_FORMAT, $resp->headers->get('Content-Type')); } + /** @test */ + public function streams_model_on_soft_delete() + { + $testModelSoftDelete = tap(TestModelSoftDelete::create(['name' => 'test']))->delete(); + + $expected = << +html; + + $resp = response()->turboStream($testModelSoftDelete); + + $this->assertEquals($expected, trim($resp->getContent())); + $this->assertEquals(Turbo::TURBO_STREAM_FORMAT, $resp->headers->get('Content-Type')); + } + /** @test */ public function streams_broadcastable_models_for_deleted() { @@ -190,6 +206,16 @@ public function hotwirePartialName() } } +class TestModelSoftDelete extends TestModel +{ + use SoftDeletes; + + public function hotwirePartialName() + { + return "_test_model_soft_delete"; + } +} + class BroadcastTestModel extends \Tonysm\TurboLaravel\Tests\TestModel { use Broadcasts; diff --git a/tests/Stubs/views/_test_model_soft_delete.blade.php b/tests/Stubs/views/_test_model_soft_delete.blade.php new file mode 100644 index 00000000..81866b10 --- /dev/null +++ b/tests/Stubs/views/_test_model_soft_delete.blade.php @@ -0,0 +1 @@ +