-
app/Events/ArticleWasApproved.php
Open in GitHub// All this event class does is accepting the article as a parameter, to pass it to the Listener final class ArticleWasApproved { use SerializesModels; public $article; public function __construct(Article $article) { $this->article = $article; } }
-
app/Providers/EventServiceProvider.php
Open in GitHub// In the EventServiceProvider, you register events and their listeners use App\Events\ArticleWasApproved; use App\Listeners\SendArticleApprovedNotification; class EventServiceProvider extends ServiceProvider { protected $listen = [ ArticleWasApproved::class => [ SendArticleApprovedNotification::class, ], // ... other events and listeners ]; }
-
app/Listeners/SendArticleApprovedNotification.php
Open in GitHub// Listener gets $event->article from the Event parameter, and sends the notification use App\Events\ArticleWasApproved; use App\Notifications\ArticleApprovedNotification; final class SendArticleApprovedNotification { public function handle(ArticleWasApproved $event): void { $event->article->author()->notify(new ArticleApprovedNotification($event->article)); } }