event and event listener laravel 11

119 Views Asked by At

I can't add constructor in my event listener.Laravel 11 there is no EventService provider also. I need an example for this

 public function handle(NewUserEvent $event): void
    {
        Mail::send('3_Emails.1_CommonMailTemplate', $mailData, function ($message) use ($Name, $Email) {
            $message->to($Email)
                ->subject("Contact | $Name")
                ->cc('[email protected]') // Add CC recipient
                ->bcc('[email protected]'); // Add BCC recipient
        });
    }
here i cant get $event in it.
2

There are 2 best solutions below

0
gokul On

Using the Event facade, you may manually register events and their corresponding listeners within the boot method of your application's AppServiceProvider

Event::listen(
    PodcastProcessed::class,
    SendPodcastNotification::class,
);
0
Abdulla Nilam On

It seems you have copied this part from somewhere. Before you come to this, there are a few things you need to do

  1. Create an event called NewUserEvent
  2. Dispatch the Event

In App\Events\NewUserEvent.php, if not, create one

namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NewUserEvent
{
    use Dispatchable, SerializesModels;

    public $name;
    public $email;

    public function __construct($name, $email)
    {
        $this->name = $name;
        $this->email = $email;
    }
}

In Controller, call the event.

event(new NewUserEvent($name, $email));

As I remember, Laravel 11 does not use EventServiceProvider; it is set to auto-discover event listeners. If not working, set it manually.