nirvair
nirvair

Reputation: 4180

Events to send mail in Laravel 5.1

I am trying to implement Events in Laravel 5.1. The event when fired will send a mail.

I did the following steps:

First, in EventServiceProvider, I added this in $listen

'send.mail' => [
            \App\Listeners\CreateTicketMail::class,
        ],

Second, created a new Event class - SendActionEmails.

And then a handler for this event in Listener class - CreateTicketMail

public function handle(SendActionEmails $event)
{
    dd($event);
}

Now when I fire this event send.mail, I get an error

Event::fire('check.fire');

Argument 1 passed to App\Listeners\CreateTicketMail::handle() must be an instance of App\Events\SendActionEmails, none given

Also, where can I send the data to the Event which will be used by the Mail. Data like to, from, subject.

One way I found that when firing event, passing as argument to the fire.

Event::fire('check.fire', array($data));

But, how do I handle this data in listener????

Upvotes: 3

Views: 1863

Answers (1)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

You need to pass event object to fire method and listen to an event named like the event class:

In EventServiceProvider:

SendActionEmails::class => [
    \App\Listeners\CreateTicketMail::class,
],

Event class:

class SendActionEmails {
  public $data;
}

Fire event and pass some data:

$event = new SendActionEmails;
$event->data = 'some data';
Event::fire($event);

Upvotes: 2

Related Questions