Event Example in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023

categoryLaravel

In this tutorial we will see event example in Laravel.

 

Event example in Laravel

In Laravel, events are a powerful way to decouple different parts of your application and allow them to communicate with each other without being tightly coupled. Here is an example of how to use events in Laravel:

Let's say we have a User model and we want to send a welcome email to the user after they register. We can define an event called "UserRegistered" in our app/Events directory:

<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Queue\SerializesModels;

class UserRegistered
{
    use SerializesModels;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

In this example, we're defining a UserRegistered event that takes a User object as a parameter. We're also using the SerializesModels trait, which Laravel uses to serialize and deserialize the model when it's passed to the event.

 

Next, we need to define a listener for the UserRegistered event. We can do this by creating a new listener class, which should be placed in the app/Listeners directory. Here's an example:

<?php

namespace App\Listeners;

use App\Events\UserRegistered;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendWelcomeEmail implements ShouldQueue
{
    public function handle(UserRegistered $event)
    {
        // send welcome email to $event->user
    }
}

In this example, we're defining a SendWelcomeEmail listener that implements the ShouldQueue interface. This means that the listener will be queued and executed asynchronously, which can help improve performance.

 

Finally, we need to trigger the UserRegistered event when a new user is registered. We can do this in our UserController:

<?php

namespace App\Http\Controllers;

use App\Events\UserRegistered;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function register(Request $request)
    {
        // create new user
        $user = new User();
        $user->name = $request->input('name');
        $user->email = $request->input('email');
        $user->password = bcrypt($request->input('password'));
        $user->save();

        // trigger UserRegistered event
        event(new UserRegistered($user));

        // return response
        return response()->json(['message' => 'User registered successfully.']);
    }
}

In this example, we're making a new User object, saving it to the database, and then executing the event method and providing it the event object to start the UserRegistered event. The user-welcome email will then be sent after the asynchronous execution of the SendWelcomeEmail listener.

 

That's a basic example of how to use events in Laravel. By using events and listeners, you can easily decouple different parts of your application and improve the overall organization and maintainability of your code.

Comments 0

Leave a comment