Backend Development

Displaying Nice Notifications in Laravel With Laravel Notify

Displaying Nice Notifications in Laravel With Laravel Notify

If you are looking for a way to display beautiful notifications in your laravel project, this package is for you mckenziearts/laravel-notify.

 

 

 

mckenziearts/laravel-notify package display a nice notification popup after every action you do in laravel controllers. The package notifications for success, errors, warnings and more.

 

Installation

Go to the github repo or run the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
composer require mckenziearts/laravel-notify
composer require mckenziearts/laravel-notify
composer require mckenziearts/laravel-notify

Register the service provider in config/app.php. For Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
'providers' => [
...
Mckenziearts\Notify\LaravelNotifyServiceProvider::class
...
];
'providers' => [ ... Mckenziearts\Notify\LaravelNotifyServiceProvider::class ... ];
'providers' => [
    ...
    Mckenziearts\Notify\LaravelNotifyServiceProvider::class
    ...
];

Publish the configuration file and assets:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
php artisan vendor:publish --provider="Mckenziearts\Notify\LaravelNotifyServiceProvider"
php artisan vendor:publish --provider="Mckenziearts\Notify\LaravelNotifyServiceProvider"
php artisan vendor:publish --provider="Mckenziearts\Notify\LaravelNotifyServiceProvider"

Finally run this command to reload the composer dependencies:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
composer dump-autoload
composer dump-autoload
composer dump-autoload

 

Config

The config file located at config/notify.php. Here you customize many things. For example you can customize the dark mode to be used by updating the theme config, or add global variable NOTIFY_THEME on your .env file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
'theme' => env('NOTIFY_THEME', 'dark'),
'theme' => env('NOTIFY_THEME', 'dark'),
'theme' => env('NOTIFY_THEME', 'dark'),

Also you can define custom user presets notifications as shown:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
'preset-messages' => [
'item-created' => [
'message' => 'The item created successfully.',
'type' => 'success',
'model' => 'connect',
'title' => 'Item Created',
]
],
'preset-messages' => [ 'item-created' => [ 'message' => 'The item created successfully.', 'type' => 'success', 'model' => 'connect', 'title' => 'Item Created', ] ],
'preset-messages' => [
    'item-created' => [
        'message' => 'The item created successfully.',
        'type'    => 'success',
        'model'   => 'connect',
        'title'   => 'Item Created',
    ]
],

 

Usage

In your blade template in the head add the styles with:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@notifyCss
@notifyCss
@notifyCss

In the footer add the scripts with:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@notifyJs
@notifyJs
@notifyJs

Then in order to show the notification add the necessary component at the bottom:

For laravel < 7

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@include('notify::components.notify')
@include('notify::components.notify')
@include('notify::components.notify')

For laravel >= 7 you can use the tag syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<x:notify-messages />
<x:notify-messages />
<x:notify-messages />

Example layout.blade.php

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
@notifyCss
</head>
<body id="page-top">
@yield('content')
@include('notify::messages')
// Laravel 7 or greater
<x:notify-messages />
@notifyJs
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> @notifyCss </head> <body id="page-top"> @yield('content') @include('notify::messages') // Laravel 7 or greater <x:notify-messages /> @notifyJs </body> </html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    @notifyCss
</head>

<body id="page-top">

    @yield('content')

    @include('notify::messages')
    // Laravel 7 or greater
    <x:notify-messages />
    
    @notifyJs
</body>
</html>

A typical scenario to fire the notification:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public function store()
{
notify()->success('Category created successfully!');
return redirect()->back();
}
public function store() { notify()->success('Category created successfully!'); return redirect()->back(); }
public function store()
{
    notify()->success('Category created successfully!');

    return redirect()->back();
}

Here we are using the helper function notify().

There are many types of notifications:

  • toast:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
notify()->success('Resource added successfully')
notify()->success('Resource added successfully')
notify()->success('Resource added successfully')
  • connectify:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
connectify('success', 'Connection Found', 'Success Message Here')
connectify('success', 'Connection Found', 'Success Message Here')
connectify('success', 'Connection Found', 'Success Message Here')
  • drakify:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
drakify('success') // for success alert
or
drakify('error') // for error alert
drakify('success') // for success alert or drakify('error') // for error alert
drakify('success') // for success alert
or
drakify('error') // for error alert
  • smilify:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
smilify('success', 'You are joined successfully')
smilify('success', 'You are joined successfully')
smilify('success', 'You are joined successfully')
  • emotify:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
emotify('success', 'Item updated with success')
emotify('success', 'Item updated with success')
emotify('success', 'Item updated with success')

 

If you want to make a shared notification that you can use across many modules without repetition, you can use a predefined preset in the config file like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
notify()->preset('item-created')
notify()->preset('item-created')
notify()->preset('item-created')

To override the preset in certain places, pass an array that has the key of the attribute that you want to override:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
notify()->preset('item-created', ['title' => 'Product created'])
notify()->preset('item-created', ['title' => 'Product created'])
notify()->preset('item-created', ['title' => 'Product created'])

 

2.8 4 votes
Article Rating

What's your reaction?

Excited
0
Happy
2
Not Sure
2
Confused
8

You may also like

Subscribe
Notify of
guest


0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments