Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fire a trashed model event and listen to it for broadcasting events #37618

Merged
merged 1 commit into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ public function onChannels(array $channels)

return $this;
}

/**
* Get the event name.
*
* @return string
*/
public function event()
{
return $this->event;
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/BroadcastsEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function bootBroadcastsEvents()
});

if (method_exists(static::class, 'bootSoftDeletes')) {
static::trashed(function ($model) {
static::softDeleted(function ($model) {
$model->broadcastTrashed();
});

Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ protected function runSoftDelete()
$query->update($columns);

$this->syncOriginalAttributes(array_keys($columns));

$this->fireModelEvent('trashed', false);
}

/**
Expand Down Expand Up @@ -136,6 +138,17 @@ public function trashed()
return ! is_null($this->{$this->getDeletedAtColumn()});
}

/**
* Register a "softDeleted" model event callback with the dispatcher.
*
* @param \Closure|string $callback
* @return void
*/
public static function softDeleted($callback)
{
static::registerModelEvent('trashed', $callback);
}

/**
* Register a "restoring" model event callback with the dispatcher.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/Integration/Database/DatabaseEloquentBroadcastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\BroadcastableModelEventOccurred;
use Illuminate\Database\Eloquent\BroadcastsEvents;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
Expand All @@ -21,6 +22,7 @@ protected function setUp(): void
Schema::create('test_eloquent_broadcasting_users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->softDeletes();
$table->timestamps();
});
}
Expand All @@ -46,6 +48,24 @@ public function testChannelRouteFormatting()

$this->assertEquals('Illuminate.Tests.Integration.Database.TestEloquentBroadcastUser.{testEloquentBroadcastUser}', $model->broadcastChannelRoute());
}

public function testBroadcastingOnModelTrashing()
{
Event::fake([BroadcastableModelEventOccurred::class]);

$model = new SoftDeletableTestEloquentBroadcastUser;
$model->name = 'Bean';
$model->saveQuietly();

$model->delete();

Event::assertDispatched(function (BroadcastableModelEventOccurred $event) {
return $event->model instanceof SoftDeletableTestEloquentBroadcastUser &&
$event->event() == 'trashed' &&
count($event->broadcastOn()) === 1 &&
$event->broadcastOn()[0]->name == 'private-Illuminate.Tests.Integration.Database.SoftDeletableTestEloquentBroadcastUser.'.$event->model->id;
});
}
}

class TestEloquentBroadcastUser extends Model
Expand All @@ -54,3 +74,10 @@ class TestEloquentBroadcastUser extends Model

protected $table = 'test_eloquent_broadcasting_users';
}

class SoftDeletableTestEloquentBroadcastUser extends Model
{
use BroadcastsEvents, SoftDeletes;

protected $table = 'test_eloquent_broadcasting_users';
}