Events

  • STEPS

    1. Events Class

    app/Events/UserRegistered.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 UserRegistered
    {
        use Dispatchable, InteractsWithSockets, SerializesModels;
    
        public $user_id;
        public $name;
        public $email;
        public $token;
    
        /**
         * Create a new event instance.
         *
         * @return  void
         */
        public function __construct($user_id, $name, $email, $token)
        {
            $this->user_id         = $user_id;
            $this->name         = $name;
            $this->email         = $email;
            $this->token         = $token;
        }
    
        // /**
        //  * Get the channels the event should broadcast on.
        //  *
        //  * @return  \Illuminate\Broadcasting\Channel|array
        //  */
        // public function broadcastOn()
        // {
        //     return new PrivateChannel('channel-name');
        // }
    }
    
    

    2. Listener Class

    app/Listeners/SendWelcomeMail.php

    
    namespace App\Listeners;
    
    use App\Events\UserRegistered;
    use App\Mails\Welcome;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Support\Facades\Mail;
    
    class SendWelcomeMail
    {
        /**
         * Create the event listener.
         *
         * @return  void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Handle the event.
         *
         * @param    \App\Events\UserRegistered  $event
         * @return  void
         */
        public function handle(UserRegistered $event)
        {
            //perfom more actions(if need be)
    
            Mail::to($event->email)->send(new Welcome($event->name, $event->token,$event->user_id ));
    
        }
    }
    

    3. Mail Class

    app/Mails/Welcome.php

    
      
    namespace App\Mails;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Mail\Mailable;
    use Illuminate\Mail\Mailables\Content;
    use Illuminate\Mail\Mailables\Envelope;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Mail\Mailables\Address;
    
    use \App\models\Notification;
    
    
    class Welcome extends Mailable
    {
        use Queueable, SerializesModels;
    
        public $data;
    
        /**
         * Create a new message instance.
         *
         * @return  void
         */
        public function __construct($name, $token, $user_id)
        {
            $this->data['name'] = $name;
            $this->data['token'] = $token;
            $this->data['user_id'] = $user_id;
        }
    
        /**
         * Get the message envelope.
         *
         * @return  \Illuminate\Mail\Mailables\Envelope
         */
        public function envelope()
        {
            return new Envelope(
                subject: 'Activation Email',
            );
        }
    
        /**
         * Get the message content definition.
         *
         * @return  \Illuminate\Mail\Mailables\Content
         */
        public function content()
        {
            $content= new Content(
                markdown: 'emails.user_activation',
                with:$this->data
            );
    
            
    
            Notification::create(
               [ 
                'user_id'=>$this->data['user_id'],
                 'content'=>$this->data['token']
               ]
            );
    
            return $content;
        }
    
        /**
         * Get the attachments for the message.
         *
         * @return  array
         */
        public function attachments()
        {
            return [];
        }
    }
    

    4. Controller

    app/Http/Controllers/

    
    
    use App\Events\UserRegistered;
    
    .... 
    .... 
    
    event(new UserRegistered( $user->id, $request->name, $request->email, $uuid));