事件未在创建的Laravel.5.6模型上触发

时间:2018-06-26 11:35:48

标签: laravel events laravel-5 eloquent

我想在公司创建的模型上触发事件。支票一切都很好。但我不知道我在误会。该事件根本没有触发。

这是我的代码。

EventServiceProvider.php

protected $listen = [
    'App\Events\NewCompany' => [
        'App\Listeners\SendWelcomeCompany',
    ],
];

事件 NewCompany.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;
use App\Company;

class NewCompany
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

   public $company;
/**
 * Create a new event instance.
 *
 * @return void
 */
public function __construct(Company $company)
{
    $this->company = $company;
}

/**
 * Get the channels the event should broadcast on.
 *
 * @return \Illuminate\Broadcasting\Channel|array
 */
public function broadcastOn()
{
    return new PrivateChannel('channel-name');
}
}

监听器 SendWelcomeCompany.php

    <?php

namespace App\Listeners;

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

class SendWelcomeCompany
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  NewCompany  $event
     * @return void
     */
    public function handle(NewCompany $event)
    {
        Mail::to($event->company->email)->send(new NewCompanyWelcome($event->company));
    }
}

公司型号 Company.php

namespace App;

use App\Events\NewCompany;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

protected $events = [
        'created' => NewCompany::class,
    ];

请告诉我我在这里想念的是什么?

谢谢

0 个答案:

没有答案