How to Create Notification using Laravel 9?
May 05, 2022 . Admin
Hello Dev,
This example is how to create notification using laravel 9?
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 9. Here you will learn how to create notification. Let's get started with how to create notification in laravel 9.
Here i will give you many example how to create notification using laravel 9.
Step 1: Download LaravelLet 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-appStep 2: Setup Database
First, you need to setup. Use this command then setup laravel project :
.envDB_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 passwordStep 3: Create Database Table
Now, we need to create "notifications" table by using laravel 9 artisan command, so let's run bellow command:
php artisan notifications:table
php artisan migrateStep 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<?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'); } /** * Write code on Method * * @return response() */ 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'); } }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);
It will help you...