Laravel 8 Notification Tutorial

Laravel 8

Nicesnippets

1166

15-10-2020


Laravel 8 Notification Tutorial

Hi Guys,

In this tutorial,I will learn you how to send notification for user in laravel 8. you can easy send notification for user in laravel 8.

Notifications are required in almost every application, especially which falls in the category of e-commerce, social media, or any noted digital product. Good news is you securely customize notification.

In this example we will create notification and send it to particular user, than we saved to database. So, you need to follow few step to make basic example with notification.

Step 1: Install Laravel Project


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

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

Step 2: Setup Database

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

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

In this step, 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

In this step, we need to create "Notification" by using laravel 5 artisan command, so let's run bellow command, we will create AlertNotification.

php artisan make:notification AlertNotification

now you can see new folder will create 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

In this is step 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

Here,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 Nicesnippests.com',

'thanks' => 'Thank you for using Nicesnippests.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);

I hope it can help you....


Recommended Posts