garf/laravel-notifications

Convenient flash notifications

2.1.1 2017-01-26 08:49 UTC

This package is auto-updated.

Last update: 2024-10-20 01:39:59 UTC


README

Laravel Version Packagist Licence Build Status

Laravel Notifications

Russian Documentation / Русская документация

Notification system for Laravel 5.

Often after saving some data from user you have to redirect to proper page and notificate user that everything done successfully or some errors appeared.

Now you can do this more convenient and easy way.

Install

To install, execute the following command in the console:

$ composer require "garf/laravel-notifications:2.*"

When completed, add to your config/app.php file in the providers section

'providers' => [
    // ...
    Garf\LaravelNotifications\LaravelNotificationsServiceProvider::class,
]

If you want to use Notifications facade, add to same file at the aliases section

'aliases' => [
    // ...
  'Notifications' => Garf\LaravelNotifications\NotificationsFacade::class,
]

To change the templates, please execute the following command in the console:

php artisan vendor:publish --provider="Garf\LaravelNotifications\LaravelNotificationsServiceProvider" --tag="config"

Now you will be able to set any view file for notifications render in config/laravel-notifications.php.

Optionally you can execute the following command in the console to edit the default template, instead of using your own:

php artisan vendor:publish --provider="Garf\LaravelNotifications\LaravelNotificationsServiceProvider" --tag="views"

Note: If you publish the view and edit it, do not change the name of the view in the config file.

Usage

Save messages for the next request

Notifications::add('Your message text', 'type', 'group');

$type param used especially for Twitter Bootstrap render. It displays alerts with respective class.

i.e. If you set type to danger, alert with class 'alert alert-danger' will be generated on toBootstrap() format method.

$group param groups messages to groups. :) On the next Request you can retrieve them by group.

Also more convenient aliases can be used:

    Notifications::info('Your message text', 'group');
    Notifications::success('Your message text', 'group');
    Notifications::warning('Your message text', 'group');
    Notifications::danger('Your message text', 'group');
    Notifications::error('Your message text', 'group');

Retrieving messages

All messages

Notifications::all()->get();

Messages by group

Notifications::byGroup('my-group')->get();

Messages by type

Notifications::byType('warning')->get();

Format messages

You also can format messages

JSON

You can retrieve and format all messages as JSON

Notifications::all()->toJson();

Or can filter them by group or type

Notifications::byType('success')->toJson();

Notifications::byGroup('login')->toJson();

Render with blade view files

You can also render your notifications with custom view files

{{ Notifications::all() }}

And you can filter them by group or type as well:

{{ Notifications::byType('info') }}

{{ Notifications::byGroup('registration') }}

by default method uses Twitter Bootstrap alerts format.

Twitter Bootstrap can be required.

Other

Count Messages

Notifications::all()->count();

Check if messages exist

Notifications::all()->has();

Get First Message

Notifications::all()->first();

Form Request usage

If you want to display errors via Laravel Form Request, you have to override method formatErrors() in your Form Request Class.

    public function formatErrors(Validator $validator){
        foreach ($validator->errors()->all() as $error) {
            Notifications::add($error, 'danger');
        }

        return $validator->errors()->getMessages();
    }

Don't forget to import Validator class in head of the file:

use Illuminate\Contracts\Validation\Validator;

Contributions

Contributions are highly appreciated.

Send your pull requests to master branch.

License

The MIT License (MIT). Please see License File for more information.