tjgazel/laravel-toastr
Laravel Toastr integrates the popular toastr.js notification library into Laravel, letting you flash session messages and trigger success, info, warning, and error toasts from controllers or Blade views through a simple, configurable API for polished in-app alerts.
This is a small, Laravel 5.x–era package that wraps the client-side Toastr library so you can flash notifications from PHP and render them in your Blade layout. With 15 stars and a 5.* constraint, treat it as a convenience wrapper you may need to fork rather than a long-term dependency.
Install
composer require tjgazel/laravel-toastr
Register the provider — On Laravel 5.5+ auto-discovery may pick it up; on 5.0–5.4 you must add it manually in config/app.php:
'providers' => [
Tjgazel\LaravelToastr\ToastrServiceProvider::class,
],
'aliases' => [
'Toastr' => Tjgazel\LaravelToastr\Facades\Toastr::class,
],
Confirm the exact namespace by reading src/ in the repo — do not trust the README blindly on a package this small.
Publish anything publishable (config and/or the Blade partial that emits the JS):
php artisan vendor:publish --provider="Tjgazel\LaravelToastr\ToastrServiceProvider"
Wire up the front end. The package does not bundle Toastr's JS/CSS for you. In your layouts/app.blade.php, load jQuery and Toastr (CDN is fine for Laravel 5 apps), then include the package's partial so flashed messages render:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
@include('laravel-toastr::partials.toastr') {{-- verify name from the repo --}}
First use case — flash a message after a redirect:
Toastr::success('Post saved.', 'Success');
return redirect()->route('posts.index');
Where to look first: the repo's README.md, then src/ for the provider, facade, and the class holding the success/error/info/warning methods, then resources/views/ for the rendering partial. Those three files tell you everything the package actually does — usually under 200 lines total.
Controller → redirect → toast. The canonical flow relies on session flash data, so the message survives one redirect. Always flash before returning the redirect:
public function store(StorePostRequest $request)
{
$post = Post::create($request->validated());
Toastr::success('Post created.', 'Success');
return redirect()->route('posts.index');
}
Chain or configure per call. Most wrappers of this style accept (message, title, options) and often return $this for chaining. Inspect the method signatures and use them consistently:
Toastr::warning('Disk usage is high.', 'Heads up', ['timeOut' => 8000, 'positionClass' => 'toast-top-center']);
Validation and error feedback. Rather than a toast per validation error, flash a single summary and let Laravel's $errors bag handle field-level messages. For exceptions, catch in a base controller or middleware and call Toastr::error() before redirecting back.
AJAX / JSON endpoints. Session flash does not help a JSON response. For XHR, either return the notification payload in your JSON (['toast' => ['type' => 'success', 'message' => ...]]) and render it client-side, or keep toast flashing strictly for full-page, redirect-based flows. Mixing the two is the most common source of "why didn't my toast show" confusion.
Centralize in a trait or base controller. If you flash the same success/error messages from dozens of methods, wrap them ($this->notifySuccess('...')) so the package can be swapped for laracasts/flash, spatie/laravel-flash, or a handwritten session helper later with one edit.
Customize the partial. The rendering Blade file is the extension point: publish it (or copy it into resources/views/vendor/...) and edit the Toastr JS options once — the toastr.options = {...} block is where progressBar, closeButton, positionClass, and timeOut usually live.
session()->flash('toastr', [...]) yourself and render a 15-line partial. The dependency is easier to reimplement than to resurrect.toastr.js) or a missing CSS file produces silent no-shows. Check the browser console first when nothing renders.Toastr::success(...) must be followed by a redirect (or a view response in the same request). If you flash and then redirect to another action that itself redirects, the flash is lost on the intermediate hop.<body>.escapeHtml: true). Passing message text with ' / < is safe but will render literally; if you want rich messages, understand that escapeHtml: false opens an XSS path — never pipe user input into it unescaped.$response->assertSessionHas('toastr') (confirm the exact session key from the source) after posting to a controller. Do not attempt to test the JS side with HTTP assertions.php artisan config:cache after publishing will freeze your options; clear it (config:clear) while iterating on toast positions/timeouts.app/Support/ if you need Laravel 6+ support.How can I help you explore Laravel packages today?