besrabasant/laravel-flash

Easy flash notifications

dev-master 2021-10-28 17:38 UTC

This package is auto-updated.

Last update: 2024-03-28 22:46:27 UTC


README

This composer package offers a Twitter Bootstrap optimized flash messaging setup for your Laravel applications.

Installation

Begin by pulling in the package through Composer.

composer require besrabasant/laravel-flash

Next, as noted above, the default CSS classes for your flash message are optimized for Bootstrap. So you need to include the styles and scripts.

Usage

Within your controllers, before you perform a redirect, make a call to the alert() or toast() function.

public function store()
{
    alert('Welcome Aboard!');

    return home();
}

or

public function store()
{
    toast('Task completed successfully!');

    return home();
}

For Alert Notifications You may also do:

  • alert('Message')->success(): Set the alert theme to "success".
  • alert('Message')->error(): Set the alert theme to "danger".
  • alert('Message')->warning(): Set the alert theme to "warning".
  • alert('Message')->overlay(): Render the message as an overlay.
  • alert()->overlay('Modal Message', 'Modal Title'): Display a modal overlay with a title.
  • alert('Message')->important(): Add a close button to the alert message.
  • alert('Message')->error()->important(): Render a "danger" alert message that must be dismissed.

With this message flashed to the session, you may now display it in your view(s). Because flash messages and overlays are so common, we provide a template out of the box to get you started. You're free to use - and even modify to your needs - this template how you see fit.

For Toast Notifications You may also do:

  • toast('Message')->success(): Set the toast theme to "success".
  • toast('Message')->error(): Set the toast theme to "danger".
  • toast('Message')->warning(): Set the toast theme to "warning".
  • toast('Message')->important(): Add a close button to the toast message.
  • toast('Message')->error()->important(): Render a "danger" toast message that must be dismissed.
@include('alert::messages')
@include('toast::container')

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>

<div class="container">
    @include('alert::messages')

    <p>Welcome to my website...</p>

    @include('toast::container')
</div>

<!-- If using alert()->important() or alert()->overlay(), you'll need to pull in the JS for Twitter Bootstrap. -->
<script src="//code.jquery.com/jquery.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<script>
    var lnAlertModal = bootstrap.Modal.getOrCreateInstance(document.getElementById('laravel_notifications__alert-overlay-modal'))
    lnAlertModal.show();
</script>

</body>
</html>

If you need to modify the alert message partials, you can run:

php artisan vendor:publish --provider="Laracasts\Flash\FlashServiceProvider"

The two package views will now be located in the resources/views/vendor/alert/ directory.

alert('Welcome Aboard!');

return home();

https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/message.png

alert('Sorry! Please try again.')->error();

return home();

https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/error.png

alert()->overlay('You are now a Laracasts member!', 'Yay');

return home();

https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/overlay.png

Learn exactly how to build this very package on Laracasts!

Hiding Flash Messages

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.

Multiple Flash Messages

Need to flash multiple flash messages to the session? No problem.

alert('Message 1');
alert('Message 2')->important();

return redirect('somewhere');

Done! You'll now see two flash messages upon redirect.