douniang3866 2017-09-23 04:39
浏览 353
已采纳

laravel model $事件未被触发

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.

  • 写回答

1条回答 默认 最新

  • duanming7833 2017-09-23 05:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vc6.0中想运行代码的时候总是提示无法打开文件是怎么回事
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题