https://github.com/apphp/laravel-flash
This package allows to use Bootstrap flash messaging for Laravel framework applications.
https://github.com/apphp/laravel-flash
apphp flash-messages laravel laravel-package laravel6 laravel6-package php
Last synced: 4 months ago
JSON representation
This package allows to use Bootstrap flash messaging for Laravel framework applications.
- Host: GitHub
- URL: https://github.com/apphp/laravel-flash
- Owner: apphp
- License: mit
- Created: 2020-03-12T20:30:53.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-24T14:46:29.000Z (almost 2 years ago)
- Last Synced: 2025-02-05T08:56:09.428Z (4 months ago)
- Topics: apphp, flash-messages, laravel, laravel-package, laravel6, laravel6-package, php
- Language: PHP
- Homepage:
- Size: 151 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://opensource.org/licenses/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](https://www.apphp.com/).## Installation
Begin by pulling in the package through Composer.
```bash
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.```html
```
## Usage
In your controllers, before you perform a redirect or render a view, make a call to the `flash()` function.
```php
public function store()
{
flash('Welcome Message!');return redirect()->route('home');
}
```The general way to define a flash message is a following:
```php
flash()->danger('The error message')->important();
flash()->info('The info message');
```
If you want to specify a title for alert, pass 2 arguments in the following way:
```php
flash()->success(['Success', 'Operation has been successfully completed']);
```
But you may use a shorter syntax:
```php
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:
```php
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:
```html
@include('flash::message')
```If you need to modify the flash message, you can run:
```bash
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.
```php
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:
```php
// 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');
// Only current message will be removed
Flash::error('Second Message')->clear();return redirect('somewhere');
```## Hide Messages
Generally you're expecting from the flash messages to be shown for a few seconds, and then they will be closed (if this message is not important).
To handle such behaviour, you may write a simple JavaScript code. For example, using jQuery, you might add the following snippet just before
the closing `
@include('flash::message')
Welcome to my website...