In this tutorial you will learn about the How to Create Send Email Notification in Laravel 8 and its application with practical example.
In this How to Create Send Email Notification in Laravel 8 tutorial I will show you how to create and send email notification in laravel 8. In this tutorial you will learn to create and send email notification in laravel 8.
How to Create & Send Email Notification in Laravel 8
In this step by step tutorial I will demonstrate you how to create and send email notification in laravel 8.
Create Laravel App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel laravel-email-notification --prefer-dist |
Setup Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db DB_USERNAME=root DB_PASSWORD= |
Generate Notification Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan notifications:table |
The above command will generate a file at path migrations/create_notifications_table.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateNotificationsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('notifications', function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('type'); $table->morphs('notifiable'); $table->text('data'); $table->timestamp('read_at')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('notifications'); } } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Create Notification Class
In this step we will create a notification class using following artisan command:
1 |
php artisan make:notification BillingNotification |
Put following code in app/Notifications/BillingNotification.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; class BillingNotification extends Notification { use Queueable; private $billData; /** * Create a new notification instance. * * @return void */ public function __construct($billData) { $this->billData = $billData; } /** * 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) ->name($this->billData['name']) ->line($this->billData['body']) ->action($this->billData['text'], $this->billData['url']) ->line($this->billData['thanks']); } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ 'bill_id' => $this->billData['bill_id'] ]; } } |
Create Controller
Now, lets create a controller named BillController using command given below –
1 |
php artisan make:controller BillController |
Put the following code in app/Http/Controllers/BillController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Notification; use App\Models\User; use App\Notifications\BillingNotification; class BillController extends Controller { public function __construct() { $this->middleware('auth'); } public function index() { return view('welcome'); } public function sendNotification() { $data = User::first(); $billData = [ 'name' => '#007 Bill', 'body' => 'You have received a new bill.', 'thanks' => 'Thank you', 'text' => '$600', 'offer' => url('/'), 'bill_id' => 30061 ]; Notification::send($data, new BillingNotification($billData)); dd('Bill notification has been sent!'); } } |
Create Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\BillController; /* |-------------------------------------------------------------------------- | 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('/', [BillController::class, 'sendNotification']); |
Send Email Notification in Laravel 8
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, visit the following URL in browser to Send Email Notification in Laravel 8
1 |
http://127.0.0.1:8000 |