A Blog Of Random Coding Techniques & Experiments Robert Askam

Create A Welcome Email with Laravel Events Post Create A Welcome Email with Laravel Events 1 year ago

What are Laravel Events?

An event is an action recognised by a program that may be handled by Laravel, this includes welcome emails, blog posts and comments. The possibilities are endless.

Why Use Events?

The reason to use events in our Laravel Application is because they create a degree of separation and gives us the ability to hook into actions in our application. You give the Event the procedure it is required to follow and therefore can be inserted into your application almost anywhere and the Event only performs the action it was created for.

Lets Begin

Create your event using the Laravel built in command.
php artisan make:event UserCreated
In app/Events you will see a new Event called UserCreated that looks like this.
namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class UserCreated
{
use InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}

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

Event Listeners

When you trigger an Event you application needs some instructions on how to handle this.
php artisan make:listener SendWelcomeEmail --event="UserCreated"
When this is created in App/Listeners you should be see this.
namespace App\Listeners;

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

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

/**
* Handle the event.
*
* @param UserCreated $event
* @return void
*/
public function handle(UserCreated $event)
{
//
}
}

Registering Events

Before we go on firing events in your application, Laravel’s uses a command bus that needs to know about the events and their listeners, and how to handle them.
To register events, we navigate to app/Providers/EventServiceProvider.php, we find the protected listen property on the EventServiceProvider class:
protected $listen = [
'App\Events\UserCreated' => [
'App\Listeners\SendWelcomeEmail',
],
];

Fire The Event

In the RegisterController.php add the Event Facade and the Event at the top.
use Event;
use App\Events\UserCreated;
Now after the user is created add this:
Event::fire(new UserCreated($user));
Now return to your App/UserCreated and accept the user into the construct:
namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class UserCreated
{
use InteractsWithSockets, SerializesModels;
public $user;

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

/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
Now in you App/SendWelcomeEmail send the email:
namespace App\Listeners;

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

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

}

/**
* Handle the event.
*
* @param UserCreated $event
* @return void
*/
public function handle(UserCreated $event)
{
$user = $event->user;
$data = array('name'=>$user->name, 'email'=>$user->email);
Mail::send('emails.newUser', $data, function($message) use ($user) {
$message->to('rbaskam@gmail.com');
$message->subject('New User Registered');
});
}
}
Create the Email Template in Resources/Views/emails/newUser.blade.php:
Hi Rob, 

//Put double curly braces around the variables
You have a new user ($name) on the site, email address is $email.

Regards,
Robert Askam