skip to Main Content

I’m a beginner of Laravel. And for the training of it, I’m learning how to use Laravel following Laravel Bootcamp.
https://bootcamp.laravel.com/blade/notifications-and-events

My code is completely same as this guide though, this program didn’t work correctly.
No mail notification is dispatched after chirping.
I guess these two of code might be suspicious.

app/Listeners/SendChirpCreatedNotifications.php

<?php
 
namespace AppListeners;
 
use AppEventsChirpCreated;
use AppModelsUser;
use AppNotificationsNewChirp;
use IlluminateContractsQueueShouldQueue;
use IlluminateQueueInteractsWithQueue;
 
class SendChirpCreatedNotifications implements ShouldQueue
{
 ...
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }
 
    /**
     * Handle the event.
     */
    public function handle(ChirpCreated $event): void
    {
        foreach (User::whereNot('id', $event->chirp->user_id)->cursor() as $user) {
            $user->notify(new NewChirp($event->chirp));
        }
    }
}

Or here.
app/Events/ChirpCreated.php

<?php
 
namespace AppEvents;
 
use AppModelsChirp;
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
 
class ChirpCreated
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
 
    /**
     * Create a new event instance.
     */
    public function __construct(public Chirp $chirp)
    {
        //
    }
 ...
    /**
     * Get the channels the event should broadcast on.
     */
    public function broadcastOn(): array
    {
        return [
            new PrivateChannel('channel-name'),
        ];
    }
 
}

I appreciate if you could help me.

Add more information(Apr 16, 2024)

"is not dispatched" means no dispatch message is issued in the log. No matter how many chirp is issued, logfile(storage/logs/laravel.log) hasn’t been appended. That means event and listener won’t work.

2

Answers


  1. Check your .env file.

    MAIL_MAILER=log
    

    In Laravel 11, the log driver is the default, so no emails are actually sent.

    Look at the log files in storage/logs/.

    Login or Signup to reply.
  2. Try to register your event and listener by adding

        protected $listen = [
        ChirpCreated::class => [
            SendChirpCreatedNotifications::class,
        ],
    ];
    

    at AppProvidersEventServiceProvider

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