I was assigned to a task to send email notification to all registered users when their thread has a new message and they weren’t able to read it. The solution was by sending them an email that has a list of all the messages that are posted not more than 1 hour on their thread.
Automatically it came to my mind to subscribe it to an event, but then since we need to send the email periodically and check for new messages and get only those messages that are posted within an hour in the thread, it would be hard for me to make it work.
Thankfully, laravel has other solution by using schedule. It allows you to fluently and expressively define your command schedule within Laravel itself. When using the scheduler, only a single Cron entry is needed on your server. Your task schedule is defined in the
schedulemethod. To help you get started, a simple example is defined within the method.
Now let start with creating php artisan command:
php artisan make:command then go to and open the file in
app\Console\Commands\SendUserUnreadMessages.php then change the value of the
$signature which is the command name of our artisan command and can also run in the terminal by typing
php artisan notification:unreadMessagesand
$description a simple sentence to know our command does.
Then, open the
app\Console\Kernel.php and then put
'App\Console\Commands\SendUserUnreadMessages' inside the
$commands array and
$schedule->command(notification:unreadMessages)->hourly(); inside the schedule() function.
I will not include the logic of getting all the users, threads and its messages as I feel that it is not related to this topic and we also use another package to handle our messenger feature. So, if we look again the SendUserUnreadMessages it now look like this.
As you can see in the handle() function, we get all the users then iterate through them using collection map then the process of checking the user’s thread if it has new messages and get those messages that posted not more than 1 hour. After that we get the messages and send the notification using
$user->notify(new UnreadMessagesInTheThread($messages, $user); but still this will not work as it was not yet created so we need to add the Notifiable Trait in our User Model and the create UnreadMessagesInTheThread notification.
Now, open the User model file which is in my case located in
app\User.php then put
use Illuminate\Notifications\Notifiable in the list of dependencies and inside the User model class
And then create a new notification by running this command
php artisan make:notification UnreadMessagesInTheThread --markdown=mails.unread-messages
Open the newly created UnreadMessagesInTheThread and copy the code below.
As you can see it has two properties
$user that will be pass into the markdown shown below, and use to let the user see all the thread’s messages. And we set a validation inside the via() method so it will not send the notification to a user who’s threads doesn’t have new messages.
That’s it! We can start sending any newsletters, subscription reminders, reports and other notifications that is scheduled to let our users feel the love without spamming them. :) Enjoy!