In your case, you may also use following approach:
// Put this code in your Article Model
public static function boot() {
parent::boot();
static::created(function($article) {
Event::fire('article.created', $article);
});
static::updated(function($article) {
Event::fire('article.updated', $article);
});
static::deleted(function($article) {
Event::fire('article.deleted', $article);
});
}
Also, you need to register listeners in AppProvidersEventServiceProvider
:
protected $listen = [
'article.created' => [
'AppHandlersEventsArticleEvents@articleCreated',
],
'article.updated' => [
'AppHandlersEventsArticleEvents@articleUpdated',
],
'article.deleted' => [
'AppHandlersEventsArticleEvents@articleDeleted',
],
];
Also make sure you have created the handlers in AppHandlersEvents
folder/directory to handle that event. For example, article.created
handler could be like this:
<?php namespace AppHandlersEvents;
use AppArticle;
use AppServicesEmailMailer; // This one I use to email as a service class
class ArticleEvents {
protected $mailer = null;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function articleCreated(Article $article)
{
// Implement mailer or use laravel mailer directly
$this->mailer->notifyArticleCreated($article);
}
// Other Handlers/Methods...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…