Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Illuminate\Database\Eloquent\Concerns;

use Closure;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Events\NullDispatcher;
use Illuminate\Events\QueuedClosure;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use ReflectionClass;
Expand Down Expand Up @@ -254,6 +256,26 @@ protected function filterModelEventResults($result)
return $result;
}

/**
* @param \Illuminate\Events\QueuedClosure|\Closure|string|array $events
* @param \Illuminate\Events\QueuedClosure|\Closure|string|array|null $callback
* @return void
*/
public static function listen($events, $callback = null)
{
if ($events instanceof Closure || $events instanceof QueuedClosure) {
$callback = $events;
$events = '*';
}

if (isset(static::$dispatcher)) {
$name = static::class;
$events = Arr::map((array) $events, fn ($event) => "eloquent.{$event}: {$name}");

static::$dispatcher->listen($events, $callback);
}
}

/**
* Register a retrieved model event with the dispatcher.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Closure;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\Fixtures\Post;

class EloquentModelListenToMultipleEventsTest extends DatabaseTestCase
{
protected function setUp(): void
{
parent::setUp();

Post::unguard();
}

protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
}

public function testEloquentModelCanListenToMultipleEvents()
{
Event::fake();

Post::listen(['saved', 'deleted'], function () {
// do something
});

$post = Post::query()->create(['title' => 'Third post']);
$post->delete();

Event::assertListening('eloquent.saved: '.Post::class, Closure::class);
Event::assertListening('eloquent.deleted: '.Post::class, Closure::class);
}

public function testEloquentModelCanListenToWildcardEvents()
{
Event::fake();

Post::listen(function () {
// do something
});

Post::query()->create(['title' => 'First post']);

Event::assertListening('eloquent.booting: '.Post::class, Closure::class);
Event::assertListening('eloquent.booted: '.Post::class, Closure::class);
Event::assertListening('eloquent.saving: '.Post::class, Closure::class);
Event::assertListening('eloquent.creating: '.Post::class, Closure::class);
Event::assertListening('eloquent.created: '.Post::class, Closure::class);
Event::assertListening('eloquent.saved: '.Post::class, Closure::class);
}
}