10-Apr-2023
.
Admin
Hi Guys,
In this tutorial,I will learn you how to send notification for user in laravel 9. you can easy send notification for user in laravel 9.
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: Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
Step 2: Database Configuration
you need the laravel fresh setup. Use this command then setup laravel project :
.env
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: Add Database Table
In this step, we need to create "notifications" table by using laravel 9 artisan command, so let's run bellow command:
php artisan notifications:table
php artisan migrate
Step 4: Add Notification
In this step, we need to create "Notification" by using laravel 9 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: Add 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
<?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: Add Controller
Here,we require to create new controller HomeController that will manage generatePDF method of route. So let's put bellow code.
php artisan make:controller HomeController
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');
}
}
Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the laravel app:
php artisan serve
Now, you have to open web browser, type the given URL and view the app output:
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....
#Laravel 9