将Eloquent事件映射到Laravel中的专用类不起作用

时间:2017-08-07 05:31:47

标签: laravel laravel-5 eloquent

我试图在创建eloquent.created模型时在Laravel 5.5中捕获Post事件,但由于任何原因它不起作用。看起来在EventServiceProvider.php中定义的事件/侦听器映射不起作用。我的设置如下。

EventServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{

    protected $listen = [
        'App\Events\PostWasPublished' => [
            'App\Listeners\NotifyBlogSubscribers',
        ]
    ];


    public function boot()
    {
        parent::boot();

        //
    }
}

NotifyBlogSubscribers.php侦听器

<?php

namespace App\Listeners;

use App\Events\PostWasPublished;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class NotifyBlogSubscribers
{

    public function __construct()
    {
        //
    }


    public function handle(PostWasPublished $event)
    {
        dd('Inside NotifyBlogSubscribers');
    }
}

PostWasPublished.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class PostWasPublished
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct()
    {
        dd('Inside PostWasPublished');
    }

    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

Post.php

<?php

namespace App\Models;


use App\Events\PostWasPublished;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {


    protected $events = [

        'created' => PostWasPublished::class,

    ];


    protected $fillable = ['user_id', 'title', 'body'];

    public function comments() {

        return $this->hasMany(Comment::class);

    }

    public function addComment($body) {

        Comment::create([
           'body' => $body,
           'post_id' => $this->id
        ]);

    }

    public function user() {

        return $this->belongsTo('App\User');

    }

    public function scopeFilter($query, $filters) {

        if (array_key_exists('month', $filters)) {

            $month = $filters['month'];

            $query->whereMonth('created_at', Carbon::parse($month)->month);

        }

        if (array_key_exists('year', $filters)) {

            $year = $filters['year'];

            $query->whereYear('created_at', $year);

        }

    }

    public static function archives() {

        return static::selectRaw('year(created_at) as year, monthname(created_at) as month, count(*) published')
            ->groupBy('year', 'month')
            ->orderByRaw('min(created_at) desc')
            ->get()
            ->toArray();
    }

    public function tags() {

        return $this->belongsToMany(Tag::class);

    }

}

如何测试? 我的第一种方法是以下列方式使用修补程序:

>>> App\Models\Post::create(['user_id' => '1', 'title' => 'some title', 'body' => 'lorem ipsum'])
=> App\Models\Post {#732
     user_id: "1",
     title: "some title",
     body: "lorem ipsum",
     updated_at: "2017-08-07 05:06:32",
     created_at: "2017-08-07 05:06:32",
     id: 62,
   }

另外,当我在前端创建帖子时,它不会遇到dd()方法。

同样composer dump-autoloadphp artisan clear-compiledphp artisan optimize没有做到这一点。知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

在Laravel 5.5中,您需要定义$dispatchesEvents属性而不是$events属性。 ($events用于较旧的Laravel版本。)