Laravel Advance | Laravel 7/8 Notification Tutorial | Create Notification with Laravel 7/8

Using laravel 6 notifications, you can send an email, send SMS, send slack message notifications to users. in this example I give you a very simple way to create the first notification to send mail in laravel 6. we can easily create Notification by the laravel artisan command. we can easily customization of notifications like mail subject, mail body, main action, etc. we almost require to use notification when we work on a large number of projects like e-commerce. might be you need to send notification for payment receipt, order place receipt, invoice, etc.

In this example we will create an email notification and send it to a particular user, then we save it to the database. So, you need to follow a few steps to make a basic example with notification.

Step 1: Install Laravel

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Database Table

php artisan notifications:tablephp artisan migrate

Step 3: Create Notification

php artisan make:notification MyFirstNotification

now you can see a new folder will create as the “Notifications” in-app folder. You need to make the following changes as like below class.

app/Notifications/MyFirstNotification.php

<?phpnamespace App\Notifications;use Illuminate\Bus\Queueable;use Illuminate\Notifications\Notification;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Messages\MailMessage;class MyFirstNotification extends Notification{use Queueable;private $details;/*** Create a new notification instance.** @return void*/public function __construct($details){$this->details = $details;}/*** Get the notification's delivery channels.** @param  mixed  $notifiable* @return array*/public function via($notifiable){return ['mail','database'];}/*** Get the mail representation of the notification.** @param  mixed  $notifiable* @return \Illuminate\Notifications\Messages\MailMessage*/public function toMail($notifiable){return (new MailMessage)->greeting($this->details['greeting'])->line($this->details['body'])->action($this->details['actionText'], $this->details['actionURL'])->line($this->details['thanks']);}/*** Get the array representation of the notification.** @param  mixed  $notifiable* @return array*/public function toDatabase($notifiable){return ['order_id' => $this->details['order_id']];}}

Step 4: Create Route

routes/web.php

Route::get('send', 'HomeController@sendNotification');

Step 4: Create Controller

app/Http/Controllers/HomeController.php

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\User;use Notification;use App\Notifications\MyFirstNotification;class HomeController extends Controller{/*** Create a new controller instance.** @return void*/public function __construct(){$this->middleware('auth');}/*** Show the application dashboard.** @return \Illuminate\Contracts\Support\Renderable*/public function index(){return view('home');}public function sendNotification(){$user = User::first();$details = ['greeting' => 'Hi Artisan','body' => 'This is my first notification from ItSolutionStuff.com','thanks' => 'Thank you for using ItSolutionStuff.com tuto!','actionText' => 'View My Site','actionURL' => url('/'),'order_id' => 101];Notification::send($user, new MyFirstNotification($details));dd('done');}}

Now we are ready to send the first notification to the user. so let’s run our example so run bellow command for a quick run

php artisan serve

you can run the following URL

http://localhost:8000/send

you can also send notifications like this way

$user->notify(new MyFirstNotification($details));

you can get sent notifications by following the command

dd($user->notifications);

--

--

raviyatechnical.blogspot.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store