Vous êtes sur la page 1sur 8

5/16/2019 Laravel 5.

7 - New Notification System Tutorial for Beginner

ItSolutionStuff.com (https://itsolutionstuff.com)

Laravel 5.7 - New Notification System Tutorial for Beginner


 By Hardik Savani (https://www.linkedin.com/in/savanihd) |  January 5, 2019 |  | 8183 Viewer |  Category : Laravel

Laravel 5 added new feature as noti cation system with mail, database, sms, markdown, broadcast, slack etc. in this tutorial i will show you simple example demo of email noti cation
system in laravel 5.7 application. you can also send all users noti cation at a time by using new noti cation system.
Laravel Noti cation can be by mail, database, sms or slack. we can easily create Noti cation by laravel artisan command. we can easily customization of noti cation like mail subject,
mail body, main action etc. we almost require to use noti cation when we work on large amount of project like e-commerce. might be you need to send noti cation for payment receipt,
order place receipt, invoice etc.
In this example we will create email noti cation and send it to particular user, than we saved to database. So, you need to follow few step to make basic example with noti cation.

Step 1: Download Laravel 5.7

I am going to explain step by step from scratch so, we need to get fresh Laravel 5.7 application using bellow command, So open your terminal OR command prompt and run bellow
command:

https://itsolutionstuff.com/post/laravel-57-new-notification-system-tutorial-for-beginnerexample.html 1/8
5/16/2019 Laravel 5.7 - New Notification System Tutorial for Beginner

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

Step 2: Create Database Table

In this step, we need to create "noti cations" table by using laravel 5 artisan command, so let's run bellow command:

php artisan notifications:table

php artisan migrate

Read Also: Laravel 5.2 chat message module using socket.io, redis, express and nodejs from from scratch. (https://itsolutionstuff.com/post/laravel-52-chat-
message-module-using-socketio-redis-express-and-nodejs-from-from-scratchexample.html)

Step 3: Create Noti cation

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

php artisan make:notification MyFirstNotification

now you can see new folder will create as "Noti cations" in app folder. You need to make following changes as like bellow class.
app/Noti cations/MyFirstNoti cation.php

https://itsolutionstuff.com/post/laravel-57-new-notification-system-tutorial-for-beginnerexample.html 2/8
5/16/2019 Laravel 5.7 - New Notification System Tutorial for Beginner

<?php

namespace App\Notifications;

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

class MyFirstNotification extends Notification


{
use Queueable;

private $details;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}

/**
* 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)
->greeting($this->details['greeting'])
->line($this->details['body'])
->action($this->details['actionText'], $this->details['actionURL'])
->line($this->details['thanks']);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
return [
'order_id' => $this->details['order_id']
];
}
}

Step 4: Create Route

In this is step we need to create routes for sending noti cation to one user. so open your "routes/web.php" le and add following route.

https://itsolutionstuff.com/post/laravel-57-new-notification-system-tutorial-for-beginnerexample.html 3/8
5/16/2019 Laravel 5.7 - New Notification System Tutorial for Beginner

routes/web.php

Route::get('send', 'HomeController@sendNotification');

Step 4: 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\User;
use Notification;
use App\Notifications\MyFirstNotification;

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 ItSolutionStuff.com',
'thanks' => 'Thank you for using ItSolutionStuff.com tuto!',
'actionText' => 'View My Site',
'actionURL' => url('/'),
'order_id' => 101
];

Notification::send($user, new MyFirstNotification($details));

dd('done');
}

Now we are ready to send rst noti cation to user. so let's run our example so run bellow command for quick run:

php artisan serve

https://itsolutionstuff.com/post/laravel-57-new-notification-system-tutorial-for-beginnerexample.html 4/8
5/16/2019 Laravel 5.7 - New Notification System Tutorial for Beginner
you can run following url:

http://localhost:8000/send

you can also send noti cation like this way:

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

you can get sent noti cations by following command:

Read Also: Laravel 5.7 - Stripe Payment Gateway Integration Example (https://itsolutionstuff.com/post/laravel-57-stripe-payment-gateway-integration-
exampleexample.html)

dd($user->notifications);

I hope it can help you....

***Do you want me hire for your Project Work? Then Contact US. (/feedback)

 Tags : Example (https://itsolutionstuff.com/tag/example.html) Laravel (https://itsolutionstuff.com/tag/laravel.html) Laravel 5 (https://itsolutionstuff.com/tag/laravel-5.html)


Laravel 5.7 (https://itsolutionstuff.com/tag/laravel-57.html) Mail (https://itsolutionstuff.com/tag/mail.html) Noti cation (https://itsolutionstuff.com/tag/noti cation.html)

Featured Post

Works with every Laravel 5 notification Build Admin Panel with Custom User Log
calendar app - Eventbot alert using bootstrap Laravel 5.8 Activity in Laravel 5
+ Slack notify plugin example App Example

Ad geteventbot.com itsolutionstuff.com itsolutionstuff.com itsolutionstuff.com

Laravel 5.6 - User Roles Laravel 5.5 Simple CMS PHP Laravel 5.7 - Create Laravel 5.7 - Generate
and Permissions (ACL) Website example using Admin Panel Example PDF from HTML
using Spatie Tutorial asgardcms Example

itsolutionstuff.com itsolutionstuff.com itsolutionstuff.com itsolutionstuff.com

We are Recommending you:

1. Laravel 5.7 Import Export Excel to database Example (https://itsolutionstuff.com/post/laravel-57-import-export-excel-to-database-


exampleexample.html)

https://itsolutionstuff.com/post/laravel-57-new-notification-system-tutorial-for-beginnerexample.html 5/8
5/16/2019 Laravel 5.7 - New Notification System Tutorial for Beginner

2. Laravel 5.7 - Create REST API with authentication using Passport Tutorial (https://itsolutionstuff.com/post/laravel-57-create-rest-api-with-
authentication-using-passport-tutorialexample.html)
3. Laravel 5.7 Autocomplete Search from Database using Typeahead JS (https://itsolutionstuff.com/post/laravel-57-autocomplete-search-from-
database-using-typeahead-jsexample.html)
4. Laravel 5.7 CRUD (Create Read Update Delete) Tutorial Example (https://itsolutionstuff.com/post/laravel-57-crud-create-read-update-delete-
tutorial-example-example.html)
5. Laravel 5 notification alert using bootstrap notify plugin example (https://itsolutionstuff.com/post/laravel-5-notification-alert-using-
bootstrap-notify-plugin-exampleexample.html)
6. Laravel notification message popup using toastr js plugin (https://itsolutionstuff.com/post/laravel-notification-message-popup-using-toastr-
js-pluginexample.html)
7. Laravel 5.2 chat message module using socket.io, redis, express and nodejs from from scratch. (https://itsolutionstuff.com/post/laravel-52-
chat-message-module-using-socketio-redis-express-and-nodejs-from-from-scratchexample.html)

Parent Notification System a Traffic to Your Web Site a

Emergency Notification System a Need a Web Designer a

Free Email Notification a Start a Blog a

Mass Notification Systems a Free Rewards a

Emergency Notification Service a Make Fast Money Online Legally a

Craftsman Tools a Dedicated Web Hosting Service a

Custom Search

Popular Posts

 laravel 5.4 New Feature - Add eloquent whereKey method (https://itsolutionstuff.com/post/laravel-54-new-feature-add-eloquent-wherekey-methodexample.html)

 Angular $scope.form value undefined on submit in ionic framework. (https://itsolutionstuff.com/post/angular-scopeform-value-undefined-on-submit-in-ionic-frameworkexample.html)

 Display preview selected image in input type file using JQuery (https://itsolutionstuff.com/post/display-preview-selected-image-in-input-type-file-using-jqueryexample.html)

 Laravel - check if folder exists before create directory (https://itsolutionstuff.com/post/laravel-check-if-folder-exists-before-create-directoryexample.html)

https://itsolutionstuff.com/post/laravel-57-new-notification-system-tutorial-for-beginnerexample.html 6/8
5/16/2019 Laravel 5.7 - New Notification System Tutorial for Beginner
 Codeigniter - Dynamic dependent dropdown using jquery ajax Example (https://itsolutionstuff.com/post/codeigniter-dynamic-dependent-dropdown-using-jquery-ajax-exampleexample.html)
 How to copy to clipboard without flash in AngularJS ? (https://itsolutionstuff.com/post/how-to-copy-to-clipboard-without-flash-in-angularjs-example.html)

 How to generate route with query string parameter in Laravel 5? (https://itsolutionstuff.com/post/how-to-generate-route-with-query-string-parameter-in-laravel-5example.html)

 How to Check if Element is Exists or not in jQuery? (https://itsolutionstuff.com/post/how-to-check-if-element-is-exists-or-not-in-jqueryexample.html)

 Laravel 5 - whereIn and whereNotIn with subquery example using query builder (https://itsolutionstuff.com/post/laravel-5-wherein-and-wherenotin-with-subquery-example-using-query-builderexample.html)

 How to get Ip Address in Laravel 5? (https://itsolutionstuff.com/post/how-to-get-ip-address-in-laravel-5example.html)


 How to use inject view in Laravel 5? (https://itsolutionstuff.com/post/how-to-use-inject-view-in-laravel-5example.html)

 Crop Image Before Upload Codeigniter 3 Example (https://itsolutionstuff.com/post/crop-image-before-upload-codeigniter-3-exampleexample.html)

Categories

Laravel (https://itsolutionstuff.com/category/laravelexample.html) PHP (https://itsolutionstuff.com/category/phpexample.html)


jQuery (https://itsolutionstuff.com/category/jquery) Bootstrap (https://itsolutionstuff.com/category/bootstrap)
Javascript (https://itsolutionstuff.com/category/javascriptexample.html) MySql (https://itsolutionstuff.com/category/mysql)
Ajax (https://itsolutionstuff.com/category/ajaxexample.html) HTML (https://itsolutionstuff.com/category/html)
Codeigniter (https://itsolutionstuff.com/category/codeigniterexample.html) AngularJS (https://itsolutionstuff.com/category/angularjsexample.html)
Vue.JS (https://itsolutionstuff.com/category/vuejsexample.html) Installation (https://itsolutionstuff.com/category/installationexample.html)
Ubuntu (https://itsolutionstuff.com/category/ubuntuexample.html) JSON (https://itsolutionstuff.com/category/jsonexample.html)
Node JS (https://itsolutionstuff.com/category/node-jsexample.html) CSS (https://itsolutionstuff.com/category/css)
Git (https://itsolutionstuff.com/category/gitexample.html) Google Map (https://itsolutionstuff.com/category/google-mapexample.html)
SQL (https://itsolutionstuff.com/category/sqlexample.html) Google API (https://itsolutionstuff.com/category/google-apiexample.html)
JQuery UI (https://itsolutionstuff.com/category/jquery-uiexample.html) Server (https://itsolutionstuff.com/category/serverexample.html)
Typeahead JS (https://itsolutionstuff.com/category/typeahead-jsexample.html)
Elasticsearch (https://itsolutionstuff.com/category/elasticsearchexample.html) React JS (https://itsolutionstuff.com/category/react-jsexample.html)
Wampserver (https://itsolutionstuff.com/category/wampserverexample.html)
Ionic Framework (https://itsolutionstuff.com/category/ionic-frameworkexample.html) MSSQL (https://itsolutionstuff.com/category/mssqlexample.html)
Socket.io (https://itsolutionstuff.com/category/socketioexample.html) Highcharts (https://itsolutionstuff.com/category/highchartsexample.html)

Latest Posts

 Custom filter/Search with Laravel Datatables Example (https://itsolutionstuff.com/post/custom-filter-search-with-laravel-datatables-exampleexample.html)

 Google maps autocomplete search only one country (https://itsolutionstuff.com/post/google-maps-autocomplete-search-only-one-countryexample.html)


 PHP - How to reindex array key after unset key? (https://itsolutionstuff.com/post/php-how-to-reindex-array-key-after-unset-keyexample.html)

 Add/remove multiple input fields dynamically with Jquery Laravel 5.8 (https://itsolutionstuff.com/post/add-remove-multiple-input-fields-dynamically-with-jquery-laravel-58example.html)

 Generate word document file in php Laravel 5 Example (https://itsolutionstuff.com/post/generate-word-document-file-in-php-laravel-5-exampleexample.html)

Advertisement

https://itsolutionstuff.com/post/laravel-57-new-notification-system-tutorial-for-beginnerexample.html 7/8
5/16/2019 Laravel 5.7 - New Notification System Tutorial for Beginner

Subscribe Your Email address:

Enter Email... Subscribe

It Solution Stuff
5.9K likes

Like Page

Be the first of your friends to like this

Top Links:
Home (https://itsolutionstuff.com)
List Of Categories (https://itsolutionstuff.com/categories)
Demo Posts (https://itsolutionstuff.com/demopost)
Latest Posts (https://itsolutionstuff.com/latestpost)
Disclaimer (https://itsolutionstuff.com/disclaimer)
Contact US (https://itsolutionstuff.com/feedback)
About US (https://itsolutionstuff.com/aboutus)

© 2016 All Rights Reserved • www.itsolutionstuff.com

https://itsolutionstuff.com/post/laravel-57-new-notification-system-tutorial-for-beginnerexample.html 8/8

Vous aimerez peut-être aussi