I am creating a new Application on which when a article is created a notification will be show. I have tried using events and listeners.
My App\Article.php
...
protected $events = [
'created' => Events\ArticleWasPublished::class
];
...
My App\Providers\EventServiceProvider.php
protected $listen = [
'App\Events\ArticleWasPublished' => [
'App\Listeners\NotifyUsers',
],
];
My App\Events\ArticleWasPublished.php
...
use App\Article;
...
{
public $article;
public function __construct(Article $article)
{
$this->article = $article;
}
...
My App\Listeners\NotifyUsers.php
use App\Notification;
...
public function handle(ArticleWasPublished $event)
{
Notification::create([
'article_id' => $event->article->id,
'message' => 'A new Article was created'
]);
var_dump('Something');
}
What am I doing wrong here?
My problem is when a new article is created a new notification is not getting created. I am not even getting any errors.
As far as I see, in Laravel 5.5 the property is named $dispatchesEvents, and not $events. Are you using this version?
https://laravel.com/docs/5.5/eloquent#events
It is one of the changes you need to do if you are migrating from an older version:
Model $events Property
The $events property on your models should be renamed to $dispatchesEvents. This change was made because of a high number of users needing to define an events relationship, which caused a conflict with the old property name.