2017 © Pedro Peláez
 

library laraflash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

image

devmarketer/laraflash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  • Wednesday, November 15, 2017
  • by jacurtis
  • Repository
  • 6 Watchers
  • 51 Stars
  • 2,442 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 14 Forks
  • 2 Open issues
  • 8 Versions
  • 18 % Grown

The README.md

LaraFlash: Improved Flash Messaging in Laravel

I have always felt that flash messaging could be improved in Laravel. The basic Session::flash wasn't terrible, but considering how much we all rely on flash notifications in our applications, I just constantly thought "There has to be a better way". LaraFlash is that way., (*1)

More importantly than letting you output flash messages, I wanted a more succinct way to work with a flash messaging object. I think the flash messaging object should be treated like an Eloquent Collection, and that is why LaraFlash was built., (*2)

You can use LaraFlash just for the LaraFlash object and manage messages yourself, or you can let this package handle everything for you, including outputting the messages to the users., (*3)

It supports several ways to work with it, to match your coding style. There are basic helper functions like flash('Something went wrong') and you can use basic facades LaraFlash::add('Something Went Wrong') or you can use a fluent syntax like: LaraFlash::new()->content('Hello World')->title('Welcome')->type('info')->priority(5);. Its up to you!, (*4)

Installation

Installation is straightforward, setup is similar to every other Laravel Package., (*5)

1. Install via Composer

Begin by pulling in the package through Composer:, (*6)

composer require devmarketer/laraflash

2. Define the Service Provider and alias

Next we need to pull in the alias and service providers., (*7)

Note: This package supports the new auto-discovery features of Laravel 5.5, so if you are working on a Laravel 5.5 project, then your install is complete, you can skip to step 3., (*8)

Inside of your config/app.php define a new service provider, (*9)

'providers' => [
    //  other providers

    DevMarketer\LaraFlash\LaraFlashServiceProvider::class,
];

Then we want to define an alias in the same config/app.php file., (*10)

'aliases' => [
    // other aliases

    'LaraFlash' => DevMarketer\LaraFlash\LaraFlashFacade::class,
];

3. Publish Config File

The config file allows you to override default settings of this package to meet your specific needs., (*11)

To generate a config file type this command into your terminal:, (*12)

php artisan vendor:publish --tag=laraflash

Usage

Using LaraFlash is easy. It extends the built in Laravel Session. Think about it as built-in, but with powerful new features., (*13)

Adding Notifications to the Session

We can break usage into two parts. First is adding to the session and the second part is extracting from the session., (*14)

There are several ways you can choose to add to the LaraFlash session. First, you can use the LaraFlash facade., (*15)

use LaraFlash;

class PageController extends Controller
{
    public function save(Request $request)
    {
        $post = Post::create($request);

        // flash message to let user know it was successful
        LaraFlash::add('Successfully Saved Post');

        return redirect(route('index'));
    }
}

That is it. You can use several methods to add a new Flash message. First you can use the add() or push() methods and pass in a string of the message you want to send., (*16)

But this only scratches the surface of what LaraFlash can do. LaraFlash is powerful because it also stores more information than just the main message. By default it also stores a priority, a type, a title, and the main message. You can pass these other options in as an $options array in the second parameter., (*17)

LaraFlash::add('You Post was Successfully Saved', array('title' => 'Congratulations! ', 'priority' => 3, 'type' => 'success'));

Now you see why this is getting awesome. Later on you can access your advanced Flash message like a record from a database. For example LaraFlash::first()->type; or LaraFlash::first()->priority;. Cool, we will talk more about rendering out your messages later, but I wanted to show you why this is cool., (*18)

Supports Multiple Flash Notifications per Request

One of the main reasons I built this package was because I often find myself needing to add multiple messages to the session. With LaraFlash you can do that, without worrying about anything., (*19)

LaraFlash::add('Message1');
LaraFlash::add('Message2');
LaraFlash::add('Message3');

Now three messages can get shown to the user. Awesome!, (*20)

But don't worry. As you will see later, displaying them to users is painless thanks to the fact that it is handled as a native Laravel Collection., (*21)

Laravel Fluent Builder

Laravel is a fluent beautiful framework, so you can build your Flash notifications in the same way. Here is the fluent chainable way to create a new LaraFlash message., (*22)

LaraFlash::new()->content('Hello World')->title('Welcome')->type('info')->priority(5);, (*23)

Its getting even better right? So easy to read and still very powerful., (*24)

You can omit properties that you do not care about, and only set the ones that are important to you. There are also shortcuts depending on the type of message you are making., (*25)

LaraFlash::success('Congratulations, we did it');
LaraFlash::info('We are using cookies');
LaraFlash::warning('Something went wrong');
LaraFlash::danger('Something just blew up');

Of course you can also chain these and multiple messages can be added to the same LaraFlash collection per request., (*26)

Helper Function

Ok so you don't have enough cool ways to add Flash Notifications to the session. I will give you one more. The helper function is often extremely popular, because it is easy to write and easy to read., (*27)

flash('Something went wrong');

That's it! Super easy to work with. Just like with the LaraFlash::add() method, you can also pass in an array of $options if you want to declare other properties. Also, multiple flash messages all go to the same LaraFlash collections, so you can mix and match any of these techniques however you want., (*28)

flash('Something went wrong', array('title' => 'Oops! ', 'type' => 'danger', 'priority' => 5));
flash('We did it!');
flash('One small problem occurred', ['type' => 'warning']);

Displaying Notifications from the Session

Of course setting notifications is easy, but at some point we need to display them to the users., (*29)

You have access to LaraFlash is any of your blade files. You can easily get all of your Flash objects with:, (*30)

{{ LaraFlash::all() }}  // All notifications
{{ LaraFlash::notifications() }}    // All notifications (same as all())
{{ LaraFlash::first() }}
{{ LaraFlash::last() }}

And you can loop through it like this:, (*31)

@foreach (LaraFlash::notifications as $notification)
    <div class="alert alert-{{ $notification->type }}" role="alert">
        <h4>{{ $notification->title }}</h4>
        <p>{{ $notification->content }}</p>
    </div>
@endforeach

Super easy huh!, (*32)

Future versions of this package will actually render out the messages for you, making it even easier. But all in due time., (*33)

Collections make life easy

Just like how I mentioned that LaraFlash extends the built-in Laravel session for storing its messages, it also extends the Laravel collection() for managing all of the messages., (*34)

Why is this important?, (*35)

Because you can treat a LaraFlash::all() notification just like an Eloquent object returned from the database. You have access to all of the same methods like ->first(), ->last(), ->nth(), ->where(), ->sortBy() and tons more. As of Laravel 5.5 there are 83 built in functions for collections that LaraFlash supports., (*36)

This means you not only have powerful data stored in your Notifications, but you can also manage, sort, re-order them, all using commands you are already familiar with, when working with your models. In fact, LaraFlash acts identical to a model or collection of records from the database., (*37)

Like I said, we just want to make Laravel flash messaging like it should be in Laravel., (*38)

Want to check out all 83 methods you can use on your LaraFlash objects? See the list of native collection methods here: https://laravel.com/docs/5.4/collections#available-methods, (*39)

Contribute

I encourage you to contribute to this package to improve it and make it better. Even if you don't feel comfortable with coding or submitting a pull-request (PR), you can still support it by submitting issues with bugs or requesting new features, or simply helping discuss existing issues to give us your opinion and shape the progress of this package., (*40)

Read the full Contribution Guide, (*41)

Contact

I would love to hear from you. I run the DevMarketer channel on YouTube, where we discuss how to "Build and Grow Your Next Great Idea" please subscribe and check out the videos., (*42)

I am always on Twitter, and it is a great way to communicate with me or follow me. Check me out on Twitter., (*43)

You can also email me at hello@jacurtis.com for any other requests., (*44)

The Versions

15/11 2017

dev-master

9999999-dev https://github.com/DevMarketer/LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by J. Alexander Curtis

laravel messages session notifications flash laraflash

15/11 2017

v1.3.1

1.3.1.0 https://github.com/DevMarketer/LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  Sources   Download

MIT

by J. Alexander Curtis

laravel messages session notifications flash laraflash

14/11 2017

v1.3.0

1.3.0.0 https://github.com/DevMarketer/LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

by J. Alexander Curtis

laravel messages session notifications flash laraflash

14/11 2017

v1.1.0

1.1.0.0 https://github.com/DevMarketer/LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

by J. Alexander Curtis

laravel messages session notifications flash laraflash

14/11 2017

v1.0.2

1.0.2.0 https://github.com/DevMarketer/LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

by J. Alexander Curtis

laravel messages session notifications flash laraflash

08/09 2017

v1.0.1

1.0.1.0 https://github.com/DevMarketer/LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by J. Alexander Curtis

laravel messages session notifications flash laraflash

07/09 2017

v1.0.0

1.0.0.0 https://github.com/DevMarketer/LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by J. Alexander Curtis

laravel messages session notifications flash laraflash

06/09 2017

v0.1.0-beta

0.1.0.0-beta https://github.com/DevMarketer/LaraFlash

A powerful and flexible flash notifications system. Improving over built-in Laravel Flash messaging functionality.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by J. Alexander Curtis

laravel messages session notifications flash laraflash