Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Toastr Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

  1. Install

    composer require tjgazel/laravel-toastr
    
  2. 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.

  3. Publish anything publishable (config and/or the Blade partial that emits the JS):

    php artisan vendor:publish --provider="Tjgazel\LaravelToastr\ToastrServiceProvider"
    
  4. 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 --}}
    
  5. 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.

Implementation Patterns

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.

Gotchas and Tips

  • Laravel 5. only.* The composer constraint will block installs on Laravel 6+. If you are on 8/9/10+, either fork and bump the constraint (usually it still works because it is thin) or just call session()->flash('toastr', [...]) yourself and render a 15-line partial. The dependency is easier to reimplement than to resurrect.
  • Assets are your responsibility. No bundled JS/CSS means a broken jQuery order (Toastr requires jQuery before toastr.js) or a missing CSS file produces silent no-shows. Check the browser console first when nothing renders.
  • Flash timing. 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.
  • Double-rendering. If your partial is included in both an authenticated layout and a guest layout, or included twice in one layout, you can get duplicate toasts. Include it exactly once, typically at the end of <body>.
  • HTML escaping. Toastr HTML-escapes by default (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.
  • Testing. Session flash is easy to assert without rendering JS: $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.
  • Config cache. On Laravel 5, php artisan config:cache after publishing will freeze your options; clear it (config:clear) while iterating on toast positions/timeouts.
  • Low maintenance signal. 15 stars and a narrow version constraint mean do not build abstractions that assume long-term upstream fixes. Pin the version, keep the package isolated behind a trait/helper, and be prepared to vendor it into app/Support/ if you need Laravel 6+ support.
  • Extension points worth knowing: the service provider (to rebind the singleton or swap the facade class), the published partial (all visual behavior lives here), and the underlying class's public methods (the only PHP-side API you should call). Everything else in the package is boilerplate.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package