2017-08-07 44 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); 

    } 

} 

我該如何測試? 我的第一種方法是通過以下方式使用補鍋匠:

>>> 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-autoload, php artisan clear-compiledphp artisan optimize沒有伎倆。任何想法我做錯了什麼?

回答

1

在Laravel 5.5中,您將需要定義$dispatchesEvents屬性而不是$events屬性。 ($events用於較舊的Laravel版本)。