Hello Devs,
In this tutorial, we will learn Laravel 8 Event Example Tutorial
The Event provides a simple observer implementation, you can subscribe and listen for events in the application.
Follow this step-by-step guide below.
Step 1: Create Event
php artisan make:event SendMail
app/Events/SendMail.php
put below code in that file.
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class SendMail
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $userId;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($userId)
{
$this->userId = $userId;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
Step 2: Listener
php artisan make:listener SendMailFired --event="SendMail"
put bellow code on app/Listeners/SendMailFired.php.
<?php
namespace App\Listeners;
use App\Events\SendMail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\User;
use Mail;
class SendMailFired
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param SendMail $event
* @return void
*/
public function handle(SendMail $event)
{
$user = User::find($event->userId)->toArray();
Mail::send('eventMail', $user, function($message) use ($user) {
$message->to($user['email']);
$message->subject('Event Testing');
});
}
}
Step 3: Providers
open app/Providers/EventServiceProvider.php and copy below code and paste it in your file
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Events\SendMail;
use App\Listeners\SendMailFired;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
SendMail::class => [
SendMailFired::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
//
}
}
Step 4:Create Route
use App\Http\Controllers\EventController;
Route::get('/event', [EventController::class, 'index'])->name('event.index');
Step 5: Create Controller
app/Http/Controllers/EventController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Event;
use App\Events\SendMail;
class EventController extends Controller
{
public function index()
{
Event::dispatch(new SendMail(1));
dd('hi');
}
}
run bellow command for quick run:
php artisan serve
open bellow URL on your browser:
localhost:8000/event
May this help you.