dimanche 6 août 2017

Map Eloquent event to a dedicated class in Laravel does not work

I am trying to catch in Laravel 5.5 the eloquent.created event when a Post model is created, but for any reason it does not work. It looks like the event/listener mapping that is defined inside the EventServiceProvider.php does not work. My setup is as follows.

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 listener

<?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);

    }

}

How do I test? My first approach is to use tinker in the following way:

>>> 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,
   }

Also, when I create a post in the front-end it does not run into the dd() methods.

Also composer dump-autoload, php artisan clear-compiled or php artisan optimize did not do the trick. Any idea what I am doing wrong?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ufvvdm
via IFTTT

Aucun commentaire:

Enregistrer un commentaire