Hey Artisan
Today i will discuss about laravel 7 notification. In this tutorial i will show you how can we send a email notification to our subscribers.
Here i will create a post table and when a new post is created then we will send laravel notification to our all subscribers. if you don't know how to send email notiication in laravel 7, then you are a right place.
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.
I will show you step by step that how you can send email laravel notification to your users. Hope you will enjoy this laravel notification tutorial.
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
After running this command you will find your newly created notification in the following directory. app/Notifications/NewPostNotify.php
Read also: Laravel 6 Notification Tutorial | Create Notification with Laravel
Step 2: Setup config/app.php
Now go to your config/app.php file and this providers.
config/app.php
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
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.
App/Notificatios/NewPostNotify.php
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 .
#laravel #laravel-notification #notification