【发布时间】:2017-09-23 04:39:17
【问题描述】:
我正在创建一个新应用程序,创建文章时将在该应用程序上显示通知。我尝试过使用事件和监听器。
我的App\Article.php
...
protected $events = [
'created' => Events\ArticleWasPublished::class
];
...
我的App\Providers\EventServiceProvider.php
protected $listen = [
'App\Events\ArticleWasPublished' => [
'App\Listeners\NotifyUsers',
],
];
我的App\Events\ArticleWasPublished.php
...
use App\Article;
...
{
public $article;
public function __construct(Article $article)
{
$this->article = $article;
}
...
我的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');
}
我在这里做错了什么?
我的问题是创建新文章时没有创建新通知。我什至没有收到任何错误。
【问题讨论】:
标签: php laravel events listener