laravel events

 event and listners

events -> its looking on whats going in

listener- it will task for the event

event group of tasks

listener-> hearing what happening, and implement the event

in office birthday parties or some activities called as event like in laravel some activities

js events-> on key up, enter,

laraqvel events

after register -> welcome message event or register email

reset pasword-> reset wvent

pay succesfful-> payment event






Laravel Events is a built-in feature of the Laravel framework that allows you to implement event-driven architecture in your application. With Laravel Events, you can define events that occur in your application, and then write listeners to handle those events.

Events are basically a trigger that occurs in your application. For example, you may want to notify your users when a new product is added to your store. This could be an event that you define in your application. To define an event, you create a new class that extends the Illuminate\Foundation\Events\Event class, and define any properties and methods that you need for the event.

Once you have defined your event, you can write listeners to handle the event. Listeners are classes that implement the Illuminate\Contracts\Events\Listener interface. They define a single handle() method that is called when the event is triggered. Inside the handle() method, you can write code to handle the event, such as sending an email notification to your users.

To dispatch an event, you simply call the event() function, passing in an instance of the event class that you defined. Laravel will automatically find and call all of the listeners that are registered to handle that event.

Laravel Events is a powerful feature that can help you write more modular and scalable code, by decoupling your application logic from the specific events that trigger it.



Sure, here's an example of how to use Laravel Events in your application:

Let's say you have an online store where users can place orders for products. You want to send an email notification to your admin whenever a new order is placed. You can use Laravel Events to implement this functionality.

First, you'll need to define an event for the "order placed" event. You can create a new event class by running the following command in your Laravel project:

csharp
php artisan make:event OrderPlaced

This will create a new OrderPlaced event class in the app/Events directory. You can define any properties and methods that you need for the event, such as the order ID and the user who placed the order:

php
namespace App\Events
use Illuminate\Foundation\Events\Dispatchable
; use Illuminate\Queue\SerializesModels
class OrderPlaced
use Dispatchable, SerializesModels
; public $orderId
; public $userId;
public function __construct($orderId, $userId)
$this->orderId = $orderId; $this->userId = $userId
 } }

Next, you'll need to define a listener for the event. You can create a new listener class by running the following command:

csharp
php artisan make:listener SendOrderNotification --event=OrderPlaced

This will create a new SendOrderNotification listener class in the app/Listeners directory. You can implement the handle() method to send the email notification:

php
namespace App\Listeners
use App\Events\OrderPlaced
use Illuminate\Contracts\Queue\ShouldQueue
use Illuminate\Queue\InteractsWithQueue
class SendOrderNotification implements ShouldQueue
use InteractsWithQueue
public function handle(OrderPlaced $event) { // Send email notification to admin // You can access the event properties like $event->orderId and $event->userId
}

Finally, you'll need to register the listener with the event in your EventServiceProvider class:

php
namespace App\Providers
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider
class EventServiceProvider extends ServiceProvider
protected $listen = [ 'App\Events\OrderPlaced' => [ 'App\Listeners\SendOrderNotification', ], ]; 
}

Now, whenever a new order is placed, you can dispatch the OrderPlaced event like this:

php
event(new OrderPlaced($orderId, $userId));

event means a liek ajob after particula thing happened means any order placesd we will that event,it will implement by listener

event called after register of person, but work by listener

step by step procedure

php artisan make:listener WelcomeEmail

php artisan make:listener SendWelcomeEmail

app/events/


<?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 WelcomeEmail
{
use Dispatchable, InteractsWithSockets, SerializesModels;

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

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



app/listeners

<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

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

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



php artisan queue:work --queue=listener

app/providers

eventserviceprovider -> register at this place event and listener

<?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;

class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array<class-string, array<int, class-string>>
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
WelcomeEmail::class => [
// ImageUpload::class,
sendWelcomeEmail::class
],
];

/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
//
}
}



working example


<?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 OrderPlaced
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $orderId;
public $userId;

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

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


app/listeners/

<?php

namespace App\Listeners;

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

class SendOrderNotification
{
use InteractsWithQueue;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param \App\Events\OrderPlaced $event
* @return void
*/
public function handle(OrderPlaced $event)
{
dd($event);
// Send email notification to admin
// You can access the event properties like $event->orderId and $event->userId
}
}


eventserviceprovider

protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
WelcomeEmail::class => [
// ImageUpload::class,
sendWelcomeEmail::class
],
'App\Events\OrderPlaced' => [
'App\Listeners\SendOrderNotification',
],
];

controller

use App\Events\OrderPlaced;
public function dummy()
{
$user = Auth::user();
$orderId=$userId = Auth::user()->id;
event(new OrderPlaced($orderId, $userId));
}


  1. Event classes stored in ______ directory.

    Marked Answer : app/Events

    Correct Answer : app/Events

  2. Listener classes stored in ______ directory.

    Marked Answer : app/Listeners

    Correct Answer : app/Listeners

  3. In which provider we have to register event listeners?

    Marked Answer : EventServiceProvider

    Correct Answer : EventServiceProvider

  4. Which interface is used to specify that a listener should be queued?

    Marked Answer : Queue

    Correct Answer : ShouldQueue

  5. By using ________ property we can specify queue connection.

    Marked Answer : $queue

    Correct Answer : $connection






No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...