PHP code example of spatie / laravel-flash

1. Go to this page and download the library: Download spatie/laravel-flash library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-flash example snippets


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

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

        return back();
    }
}

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

        flash('My message');

        return back();
    }
}

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

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

        return back();
    }
}

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

// this would probably go in a service provider

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

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

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

// 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'));
});

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

// 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`