Automatically flash success alerts in resources controllers: `store`, `update` and `destroy` methods.
royvoetman/laravel-flash-alerts is a Laravel package for automatically flash success alerts in resources controllers: `store`, `update` and `destroy` methods..
It currently has 4 GitHub stars and 25 downloads on Packagist (latest version v1.0.6).
Install it with composer require royvoetman/laravel-flash-alerts.
Discover more Laravel packages by royvoetman
or browse all Laravel packages to compare alternatives.
Last updated
This packages will automatically flash success messages to the session when a controller's: store, update or destroy method was successfully executed.
An execution is considered successful when no exceptions were raised and no messages are defined in the current session under the key warning.
composer require royvoetman/laravel-flash-alerts
Add FlashAlerts middleware to the routeMiddleware array in app/Http/Kernel.php
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
...
'flash.alerts' => \RoyVoetman\LaravelFlashAlerts\Middleware\FlashAlerts::class
];
Add the FlashesAlerts trait to your applications BaseController
<?php
namespace App\Http\Controllers;
use RoyVoetman\LaravelFlashAlerts\Traits\FlashesAlerts;
...
abstract class Controller extends BaseController
{
use FlashesAlerts;
...
}
public function registerAlertMiddleware(string $model, array $except = []);
$model = 'Book' will result in: The Book has been successfully addedIf the middleware is registered and a store, update or destory method has been successfully executed.
A message will be flashed in the current session under the key alert.
If the current session has a message under the key warning or an exception is thrown, the request will not be considered successful.
<?php
namespace App\Http\Controllers;
class BookController extends Controller
{
/**
* BookController constructor.
*/
public function __construct()
{
parent::__construct();
$this->registerAlertMiddleware('Book');
}
...
}
<?php
namespace App\Http\Controllers;
class BookController extends Controller
{
/**
* BookController constructor.
*/
public function __construct()
{
parent::__construct();
$this->registerAlertMiddleware('Book', ['destroy']);
}
public function store()
{
// Will flash alert
return redirect()->route('books.index');
}
public function destroy()
{
// Won't flash alert
return redirect()->route('books.index');
}
...
}
@if (session()->has('alert'))
<div class="alert alert-success" role="alert">
{{ session('alert') }}
</div>
@endif
php artisan vendor:publish --provider="RoyVoetman\LaravelFlashAlerts\FlashAlertsServiceProvider"
This will place the overwritable translations under resources/lang/vendor/laravel-flash-alerts
Please see CHANGELOG for more information what has changed recently.
Contributions are welcome and will be fully credited. We accept contributions via Pull Requests on Github.
README.md and any other relevant documentation are kept up-to-date.The MIT License (MIT). Please see License File for more information.