Skip to content

This package allows to use Bootstrap flash messaging for Laravel framework applications.

License

Notifications You must be signed in to change notification settings

apphp/laravel-flash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT

Simple Flash Messages for Laravel Framework Applications

This package allows to use Bootstrap 3/4/5 flash messaging for Laravel 6+ framework applications.

Requirements

  • PHP >=7.1
  • Laravel 6+
  • Bootstrap 3+

License

This project is released under the MIT License.
Copyright © 2020 ApPHP.

Installation

Begin by pulling in the package through Composer.

composer require apphp/laravel-flash

Next, make sure the default CSS classes for your flash message are optimized for Bootstrap. You may either pull in the Bootstrap's CSS within your HTML or layout file, or write your own CSS classes based on them. If you use Bootstrap 3, part of classes, like "primary" and "secondary" will not have styling.

<link rel="stylesheet" href="//getbootstrap.com/docs/4.0/dist/css/bootstrap.min.css">

Usage

In your controllers, before you perform a redirect or render a view, make a call to the flash() function.

public function store()
{
    flash('Welcome Message!');

    return redirect()->route('home');
}

The general way to define a flash message is a following:

flash()->danger('The error message')->important();
flash()->info('The info message');

Simple message

If you want to specify a title for alert, pass 2 arguments in the following way:

flash()->success(['Success', 'Operation has been successfully completed']);

Message with title

But you may use a shorter syntax:

flash(['Error', 'The error message'], 'error', true);
flash('The info message', true);
flash('The info message');

You may also define the following flash messages:

Method Description
flash('your-message', 'primary') Set the flash type to "primary".
flash('your-message', 'secondary') Set the flash type to "secondary".
flash('your-message', 'success') Set the flash type to "success".
flash('your-message', 'warning') Set the flash type to "warning".
flash('your-message', 'validation') Set the flash type to "validation".
flash('your-message', 'info') Set the flash type to "info".
flash('your-message', 'danger') Set the flash type to "danger".
flash('your-message', 'error') Set the flash type to "error" (alias to "danger") w/o a close button.
flash('your-message', 'error', true) Set the flash type to "error" with a close button to the message.
flash('your-message', 'light') Set the flash type to "light".
flash('your-message', 'dark') Set the flash type to "dark".

You may also define messages, by using Flash facade:

use Apphp\Flash\Flash;
Method Description
Flash::success('your-message') Set the success flash message.
Flash::error('your-message') Set the flash type to "error" w/o a close button to the message.
Flash::error('your-message', true) Set the flash type to "error" with a close button to the message.
etc.

To show messages on view files, use the following:

@include('flash::message')

If you need to modify the flash message, you can run:

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

Show Multiple Messages

If you need to flash multiple flash messages, you may simply define them one after another.

flash('First Message', 'success');
flash('Second Message', 'warning', true);

return redirect('somewhere');

Take in account, that you'll not see flash messages if you don't perform redirect.

Clear Messages

If you need to clear flash messages, you may do it in the following way:

// All previously defined messages will be removed
flash('First Message', 'error');
flash('Second Message', 'danger')->clear();

// All previously defined messages will be removed
flash('First Message', 'error');
flash('Second Message', 'danger');
Flash::success('Third Message');
flash()->clear();

Flash::success('First Message');