2017-08-07 5 views
1

Postモデルが作成されたときにLaravel 5.5 eloquent.createdイベントをキャッチしようとしていますが、何らかの理由で動作しません。 EventServiceProvider.phpの内部で定義されているイベント/リスナーのマッピングが機能しないようです。私の設定は以下の通りです。地図Laravelの専用クラスへの奇妙なイベントが機能しません

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

    } 

} 

どのようにテストしますか?私はフロントエンドで投稿を作成する際には、DD()メソッドに実行されません、

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

また: 私の最初のアプローチは、次のようにいじくりを使用することです。

また、composer dump-autoloadphp artisan clear-compiledまたはphp artisan optimizeはこのトリックを行いませんでした。私が間違って何をしているのか?

答えて

1

Laravel 5.5では、$eventsプロパティの代わりに$dispatchesEventsプロパティを定義する必要があります。 (より古いLaravelバージョンでは$eventsが使用されました)。

関連する問題