All Projects → spatie → Laravel Flash

spatie / Laravel Flash

Licence: mit
A lightweight package to flash messages

Projects that are alternatives of or similar to Laravel Flash

Laraflash
⚡ Flash messages on steroids.
Stars: ✭ 150 (-60.73%)
Mutual labels:  laravel, flash
Laravel Websockets
Websockets for Laravel. Done right.
Stars: ✭ 4,157 (+988.22%)
Mutual labels:  laravel
Kupo
✅ Simple site launch checklist checker/validator.
Stars: ✭ 370 (-3.14%)
Mutual labels:  laravel
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (-1.83%)
Mutual labels:  laravel
Venture
Venture allows you to create and manage complex, async workflows in your Laravel apps.
Stars: ✭ 374 (-2.09%)
Mutual labels:  laravel
Docker Stacks
DECK is a powerful and high performant local web development studio unlike any other.
Stars: ✭ 376 (-1.57%)
Mutual labels:  laravel
Laravel Pivot
This package introduces new events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.
Stars: ✭ 370 (-3.14%)
Mutual labels:  laravel
Laravel Report Generator
Rapidly Generate Simple Pdf, CSV, & Excel Report Package on Laravel
Stars: ✭ 380 (-0.52%)
Mutual labels:  laravel
Whisper
📣 Whisper is a component that will make the task of display messages and in-app notifications simple. It has three different views inside
Stars: ✭ 3,718 (+873.3%)
Mutual labels:  message
Laravel Activitylog
Log activity inside your Laravel app
Stars: ✭ 4,123 (+979.32%)
Mutual labels:  laravel
Laravel Pt Br Localization
Tradução do Laravel para português brasileiro (pt_BR locale)
Stars: ✭ 373 (-2.36%)
Mutual labels:  laravel
Laravel
[DEPRECATED] See https://github.com/lucidarch/lucid
Stars: ✭ 373 (-2.36%)
Mutual labels:  laravel
Laravel Firebase
A Laravel package for the Firebase PHP Admin SDK
Stars: ✭ 369 (-3.4%)
Mutual labels:  laravel
Forge Sdk
PHP SDK for managing Laravel Forge servers
Stars: ✭ 371 (-2.88%)
Mutual labels:  laravel
Coastercms
The repository for Coaster CMS (coastercms.org), a full featured, Laravel based Content Management System
Stars: ✭ 380 (-0.52%)
Mutual labels:  laravel
Graphqlite
Use PHP Annotations to declare your GraphQL API
Stars: ✭ 370 (-3.14%)
Mutual labels:  laravel
Laravel Validation Rules
A set of useful Laravel validation rules
Stars: ✭ 374 (-2.09%)
Mutual labels:  laravel
Cipi
An Open Source Control Panel for your Cloud! Deploy and manage LEMP apps in one click!
Stars: ✭ 376 (-1.57%)
Mutual labels:  laravel
Google Maps
Collection of Google Maps API Web Services for Laravel
Stars: ✭ 380 (-0.52%)
Mutual labels:  laravel
Secure Headers
PHP Secure Headers
Stars: ✭ 379 (-0.79%)
Mutual labels:  laravel

A lightweight package to flash messages

Latest Version on Packagist run-tests Total Downloads

This is a lightweight package to send flash messages in Laravel apps. A flash message is a message that is carried over to the next request by storing it in the session. This package only supports one single flash message at a time.

This is how it can be used:

class MySpecialSnowflakeController
{
    public function store()
    {
        // …

        flash('My message', 'my-class');

        return back();
    }
}

In your view you can do this:

@if (flash()->message)
    <div class="{{ flash()->class }}">
        {{ flash()->message }}
    </div>
@endif

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-flash

Usage

Here is an example on how to flash a message.

class MyController
{
    public function store()
    {
        // …

        flash('My message');

        return back();
    }
}

In your view you can use it like this

@if(flash()->message)
    <div>
        {{ flash()->message }}
    </div>
@endif

Using a class name to style the displayed message

You can add a class as the second parameter. This is typically used to style the output in your HTML.

class MyController
{
    public function store()
    {
        // …

        flash('My message', 'my-class');

        return back();
    }
}

In your view you can use the class like this:

@if (flash()->message)
    <div class="{{ flash()->class }}">
        {{ flash()->message }}
    </div>
@endif

You can also set an array of classes. These will be output by flash()->class by imploding the array with a space-delimiter.

flash('My message', ['my-class', 'another-class']); // flash()->class output is: 'my-class another-class'

Adding your own methods

If you don't want to specify a class each time you flash a message you can add a method name to flash.

The easiest way is by passing an array to the levels method. The key is the method name that should be added to flash(). The value is the class that will automatically be used when rendering the message.

// this would probably go in a service provider

\Spatie\Flash\Flash::levels([
    'success' => 'alert-success',
    'warning' => 'alert-warning',
    'error' => 'alert-error',
]);

The above example will make these methods available on flash:

flash()->success('Hurray');
flash()->warning('Mayybeee');
flash()->error('Oh Oh');

The most likely scenario is that you want to consume the flash message in a view. You can use the message, class and level properties on the view.

@if (flash()->message)
    <div class="{{ flash()->class }}">
        {{ flash()->message }}
    </div>

    @if(flash()->level === 'error')
        This was an error.
    @endif
@endif

Additionally, when you've added your own method, you can also pass that method name as a second parameter to flash itself:

flash('Hurray', 'success'); // `flash()->class` will output 'alert-success'

You can also add a method to flash by using macro.

Here's an example:

// this would probably go in a service provider

use Spatie\Flash\Message;

\Spatie\Flash\Flash::macro('warning', function (string $message) {
    return $this->flashMessage(new Message($message, 'alert alert-warning'));
});

You can now use a warning method on flash:

flash()->warning('Look above you!');

You can pass the desired level as the third argument to Message.

// in a service provider
use Spatie\Flash\Message;

\Spatie\Flash\Flash::macro('warning', function (string $message) {
    return $this->flashMessage(new Message($message, 'alert alert-warning', 'my-level'));
});

// in the next request, after having flashed a message 
flash()->level; // returns `my-level`

Alternatives

This package is intended to be lightweight. If you need things like multiple messages, support for Bootstrap, overlays, ... take a look at this excellent flash package by Jeffrey Way or Laraflash by Ilya Sakovich.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].