PHPackages                             spatie/laravel-flash - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/laravel-flash

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/laravel-flash
====================

A lightweight package to flash messages

1.10.2(2mo ago)6631.8M↓11.7%3314MITPHPPHP ^7.4|^8.0CI passing

Since Mar 14Pushed 2mo ago10 watchersCompare

[ Source](https://github.com/spatie/laravel-flash)[ Packagist](https://packagist.org/packages/spatie/laravel-flash)[ Docs](https://github.com/spatie/laravel-flash)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/spatie-laravel-flash/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (22)Used By (14)

A lightweight package to flash messages
=======================================

[](#a-lightweight-package-to-flash-messages)

[![Latest Version on Packagist](https://camo.githubusercontent.com/238f3b3166f0ebccaf95a0730f53e43367d29cac76dc5e061778e2f63c7e2229/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d666c6173682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-flash)[![run-tests](https://github.com/spatie/laravel-flash/workflows/run-tests/badge.svg)](https://github.com/spatie/laravel-flash/workflows/run-tests/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/2d8e7f3c419aac17f5229fe2d1d7ed62d47168db7418b4ec2700a383e5e0005d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d666c6173682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-flash)

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)

        {{ flash()->message }}

@endif
```

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/cca31a35ddf37d5b4fc4f8f69f21fc12b23264ab12da80a39ac14cb366907224/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d666c6173682e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-flash)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-flash
```

Usage
-----

[](#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)

        {{ flash()->message }}

@endif
```

### Using a class name to style the displayed message

[](#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)

        {{ flash()->message }}

@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

[](#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)

        {{ flash()->message }}

    @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
------------

[](#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](https://github.com/laracasts/flash) by [Jeffrey Way](https://github.com/JeffreyWay) or [Laraflash](https://github.com/coderello/laraflash) by [Ilya Sakovich](https://github.com/hivokas).

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

### Security

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Postcardware
------------

[](#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](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

67

—

FairBetter than 100% of packages

Maintenance83

Actively maintained with recent releases

Popularity62

Solid adoption and visibility

Community34

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 74.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~133 days

Recently: every ~375 days

Total

20

Last Release

86d ago

Major Versions

0.0.2 → 1.0.02019-03-14

0.0.3 → 1.1.02019-03-14

PHP version history (4 changes)0.0.1PHP ^7.1

1.4.0PHP ^7.4

1.8.0PHP ^7.4 || ^8.0

1.9.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (95 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (7 commits)")[![faustbrian](https://avatars.githubusercontent.com/u/22145591?v=4)](https://github.com/faustbrian "faustbrian (5 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (4 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (2 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (1 commits)")[![benjamincrozat](https://avatars.githubusercontent.com/u/3613731?v=4)](https://github.com/benjamincrozat "benjamincrozat (1 commits)")[![jrmajor](https://avatars.githubusercontent.com/u/26096713?v=4)](https://github.com/jrmajor "jrmajor (1 commits)")[![davidmuggleton](https://avatars.githubusercontent.com/u/15907082?v=4)](https://github.com/davidmuggleton "davidmuggleton (1 commits)")[![paulredmond](https://avatars.githubusercontent.com/u/177773?v=4)](https://github.com/paulredmond "paulredmond (1 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (1 commits)")

---

Tags

flashlaravelmessagephpspatielaravel-flash

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-laravel-flash/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-laravel-flash/health.svg)](https://phpackages.com/packages/spatie-laravel-flash)
```

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M57](/packages/spatie-laravel-analytics)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
