Surfe.be - passive income

Laravel Notification Examples from Scratch

Laravel notification System

In addition to support for sending email, Laravel provides support for sending notifications across a variety of delivery channels, including mail, SMS (via Nexmo), and Slack. Notifications may also be stored in a database so they may be displayed in your web interface.Typically, notifications should be short, informational messages that notify users of something that occurred in your application. For example, if you are writing a billing application, you might send an "Invoice Paid" notification to your users via the email and SMS channels.

laravel notification examples from scratch

New Post Notification Mail

Now i am going to create a new post  notification system in laravel . When we will create a new post then our all subscriber will be notified. Let’s see how we can do it.

Step 1:  Creating Notification

In Laravel, each notification is represented by a single class (typically stored in the app/Notifications directory). Don't worry if you don't see this directory in your application, it will be created for you when you run the make:notification Artisan command:

php artisan make:notification NewPostNotify

This command will place a fresh notification class in your app/Notifications directory. Each notification class contains a via method and a variable number of message building methods (such as toMail or toDatabase) that convert the notification to a message optimized for that particular channel.

Step 2: Setup config/app.php

Now go to your config/app.php file and this providers

Illuminate\Notifications\NotificationServiceProvider::class

Now we have to setup our controller . So go to your controller and paste the following code

Step 3 : Setup Controller

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\user\Post;
use App\Notifications\NewPostNotify;
use Illuminate\Support\Facades\Notification;
use App\Model\user\Subscriber;
use Illuminate\Notifications\Notifiable;

class PostController extends Controller{    

    public function store(Request $request)
    {
       //Save your new post here . after posting then send mail to the subscriber
 
        $subscribers = Subscriber::all(); //Retrieving all subscribers 
      
        foreach($subscribers as $subscriber){
            Notification::route('mail' , $subscriber->email) //Sending mail to subscriber 
                          ->notify(new NewPostNotify($posts)); //With new post

        return redirect()->back();
      }
    }
}

Step 4 : Setup App/Notificatios/NewPostNotify.php

Now finally Setup your Notification file . Go you App/Notificatios/NewPostNotify.php file and paste those following code.

class NewPostNotify extends Notification
{
    use Queueable;

    public $post;
   
    public function __construct($post)
    {
       $this->post = $post; //Catching New Post 
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->subject('Hey user, New post availabe')
                    ->greeting('Hello' , 'Subscriber')
                    ->line('There is a new post , hope you will like it')
                    ->line('Post title : '.$this->post->title) //Send with post title
                    ->action('Read Post' , url(route('post' , $this->post->slug))) //Send with post url
                    ->line('Thank you for being with us!');
    }

}

If your mail server is ok then now you can send notification to your subscriber.

If you have many subscriber then you should use laravel queue to get better performance of your application . If you don’t know how to configure laravel queue then follow this tutorial

How to configure Laravel Queue

If you want to learn more about notification then visit here and read this documentation That’s it . Hope you will enjoy it. If you like this tutorial then don’t forget to share and share your experience with me.

Leave a comments

LET'S SOCIALITE

Recent Tweets