skip to Main Content

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

Answers


  1. 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,
    );
    
    Login or Signup to reply.
  2. 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 AppEventsNewUserEvent.php, if not, create one

    namespace AppEvents;
    
    use IlluminateFoundationEventsDispatchable;
    use IlluminateQueueSerializesModels;
    
    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search