Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

laravelio/laravel.io

2497 stars
3 code files
View laravelio/laravel.io on GitHub

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));
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.