First, pull in the package through Composer.
Run composer require laracasts/flash
And then, if using Laravel 5, include the service provider within config/app.php.
'providers' => [
Laracasts\Flash\FlashServiceProvider::class,
];Within your controllers, before you perform a redirect...
publicfunctionstore()
{
flash('Welcome Aboard!');
returnhome();
}You may also do:
flash('Message', 'title', 'info')flash('Message', 'title', 'success')flash('Message', 'title', 'danger')flash('Message', 'title', 'warning')flash()->overlay('Modal Message', 'Modal Title')flash('Message', 'title')->important()
Behind the scenes, this will set a few keys in the session:
- 'flash_notification.message' - The message you're flashing
- 'flash_notification.title' - The title you're flashing
- 'flash_notification.level' - A string that represents the type of notification (good for applying HTML class names)
With this message flashed to the session, you may now display it in your view(s). Maybe something like:
@if (session()->has('flash_notification.message'))
<divclass="alert alert-{{ session('flash_notification.level') }}"><buttontype="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{!! session('flash_notification.message') !!}
</div>
@endifNote that this package is optimized for use with Twitter Bootstrap.
Because flash messages and overlays are so common, if you want, you may use (or modify) the views that are included with this package. Simply append to your layout view:
@include('flash::message')<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Document</title><linkrel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"></head><body><divclass="container">
@include('flash::message')
<p>Welcome to my website...</p></div><!-- This is only necessary if you do Flash::overlay('...') --><scriptsrc="//code.jquery.com/jquery.js"></script><scriptsrc="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script><script>$('#flash-overlay-modal').modal();</script></body></html>If you need to modify the flash message partials, you can run:
php artisan vendor:publishThe two package views will now be located in the app/views/packages/laracasts/flash/ directory.
flash('Welcome Aboard!');
returnhome();flash('Sorry! Please try again.', 'danger');
returnhome();flash()->overlay('Notice', 'You are now a Laracasts member!');
returnhome();A common desire is to display a flash message for a few seconds, and then hide it. To handle this, write a simple bit of JavaScript. For example, using jQuery, you might add the following snippet just before the closing </body> tag.
<script>
$('div.alert').not('.alert-important').delay(3000).fadeOut(350);
</script>
This will find any alerts - excluding the important ones, which should remain until manually closed by the user - wait three seconds, and then fade them out.


