How To Create Notification Using Laravel 8?


Share:
Published By: Admin, Published On: Dec 15, 2021, Category: Laravel 8 Laravel


Hello Dev,

This example is how to create notification using laravel 8?

Now let's see example of how to create notification example. We will check how to create notification. This is a short guide on create notification in laravel 8. Here you will learn how to create notification. Let's get started with how to create notification in laravel 8.

Here i will give you many example how to create notification using laravel 8.

Step 1 : Install Laravel Project

Let's First, Use this command then download laravel project setup :

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Setup Database

First, you need to setup. Use this command then setup laravel project :

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=here your database name 
DB_USERNAME=here database username
DB_PASSWORD=here database password 
Step 3 : Create Database Table

Now, we need to create "notifications" table by using laravel 5 artisan command, so let's run bellow command:

php artisan notifications:table
php artisan migrate
Step 4 : Create Notification

Now, we will create AlertNotification.

php artisan make:notification AlertNotification

Let's create new folder as "Notifications" in app folder. You need to make following changes as like bellow class.

app/Notifications/AlertNotification.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class AlertNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)                    
            ->name($this->offerData['name'])
            ->line($this->offerData['body'])
            ->action($this->offerData['offerText'], $this->offerData['offerUrl'])
            ->line($this->offerData['thanks']);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'offer_id' => $this->offerData['offer_id']
        ];
    }
}
Step 5 : Create Route

Now, we need to create routes for sending notification to one user. so open your "routes/web.php" file and add following route.

routes/web.php

use App\Http\Controllers\HomeController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('send', [HomeController::class,'sendNotification']);
Step 6 : Create Controller

In this step, we require to create new controller HomeController that will manage generatePDF method of route. So let's put bellow code.

app/Http/Controllers/HomeController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use Notification;
use App\Notifications\AlertNotification;
  
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 MyWebtuts.com',
            'thanks' => 'Thank you for using MyWebtuts.com tuto!',
            'actionText' => 'View My Site',
            'actionURL' => url('/'),
            'order_id' => 101
        ];
  
        Notification::send($user, new AlertNotification($details));
   
        dd('done');
    }
  
}

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/send

you can also send notification like this way:

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

you can get sent notifications by following command:

dd($user->notifications);

It will help you...

Share: