Weave Code
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 Toaster Magic Laravel Package

devrabiul/laravel-toaster-magic

Dependency-free toast notifications for Laravel with Livewire v3/v4 support. Drop-in, customizable toasts with multiple modern themes, RTL + dark mode, XSS-safe links, and no need for jQuery, Bootstrap, or Tailwind.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require devrabiul/laravel-toaster-magic
    php artisan vendor:publish --provider="Devrabiul\ToastMagic\ToastMagicServiceProvider"
    

    (Assets auto-publish on first page load.)

  2. Add to Blade:

    <head>
        {!! ToastMagic::styles() !!}
    </head>
    <body>
        {!! ToastMagic::scripts() !!}
    </body>
    
  3. First Toast:

    use Devrabiul\ToastMagic\Facades\ToastMagic;
    ToastMagic::success('Success!', 'Your action was completed.');
    

Where to Look First

  • Config: config/laravel-toaster-magic.php (position, theme, Livewire settings).
  • Facade: Devrabiul\ToastMagic\Facades\ToastMagic (core API).
  • Livewire: this->dispatch('toastMagic', [...]) (event-based usage).

First Use Case

Trigger a success toast after a form submission in a controller:

public function store(Request $request) {
    $request->validate([...]);
    ToastMagic::success('Saved!', 'Your data is now stored.');
    return back();
}

Implementation Patterns

Core Workflows

  1. Controller Integration:

    // Static method (quick)
    ToastMagic::warning('Warning!', 'Check your inputs.', ['timeOut' => 8000]);
    
    // Fluent method (customizable)
    ToastMagic::dispatch()
        ->error('Failed', 'Something went wrong.')
        ->withOptions(['theme' => 'neon', 'positionClass' => 'toast-bottom-end']);
    
  2. Livewire Integration:

    // Component method
    public function createUser() {
        $this->validate([...]);
        $this->dispatch('toastMagic', [
            'status' => 'success',
            'title' => 'User Created',
            'message' => 'Profile saved.',
            'options' => ['customBtnText' => 'View', 'customBtnLink' => route('profile')],
        ]);
    }
    
  3. JavaScript Integration:

    const toast = new ToastMagic();
    toast.info('Update Available', 'New version ready.', false, 'Download', '/update');
    

Integration Tips

  • Theme Consistency: Set a default theme in config and override per-toast if needed:
    ToastMagic::success('...', '...', ['theme' => 'glassmorphism']);
    
  • Positioning: Use positionClass for dynamic layouts (e.g., mobile vs. desktop):
    if (request()->isMobile()) {
        ToastMagic::setOption('positionClass', 'toast-bottom-start');
    }
    
  • Livewire Events: Listen globally in a Livewire component:
    protected $listeners = ['toastMagic' => 'handleToast'];
    public function handleToast($event) {
        $this->dispatchBrowserEvent('toastMagic', $event);
    }
    
  • Validation Feedback: Chain toasts with validation errors:
    if ($errors->any()) {
        foreach ($errors->all() as $error) {
            ToastMagic::error('Validation Error', $error);
        }
    }
    

Gotchas and Tips

Pitfalls

  1. XSS Risks:

    • Gotcha: Custom button links (customBtnLink) are sanitized to prevent javascript: injection.
    • Fix: Only http://, https://, /, or # URLs are allowed. Invalid links default to #.
  2. Livewire Version Mismatch:

    • Gotcha: Livewire v3/v4 dispatch syntax differs slightly. Ensure livewire_version in config matches your project.
    • Fix: Test in a component first:
      $this->dispatch('toastMagic', ['status' => 'info']);
      
  3. Caching Issues:

    • Gotcha: php artisan migrate may fail if CACHE_DRIVER=database and the cache table doesn’t exist.
    • Fix: The package now gracefully falls back to non-cached computation during migrations.
  4. Message Truncation:

    • Gotcha: rtrim() was corrupting messages with <br> tags (e.g., "error" → "err").
    • Fix: Updated to preg_replace('/(<br>)+$/', '', $string) in all toast methods.

Debugging

  • Toast Not Showing?:

    • Verify ToastMagic::styles() and ToastMagic::scripts() are in <head>/</body>.
    • Check browser console for JS errors (e.g., missing Livewire JS).
    • Ensure no ad-blockers are blocking the toast container.
  • Livewire Not Working:

    • Confirm livewire_enabled: true in config.
    • Check Livewire version (v3 or v4) matches your project.
    • Listen for events in a component:
      protected $listeners = ['toastMagic' => 'showToast'];
      public function showToast($event) { dd($event); }
      

Config Quirks

  • Gradient Mode: Only works with default, material, or neumorphism themes.
  • Dark Mode: Requires theme="dark" on <body> (not a config option).
  • Position Classes: Must match Bootstrap toast positions.

Extension Points

  1. Custom Themes:

    • Override default themes by publishing assets:
      php artisan vendor:publish --tag=laravel-toaster-magic-assets --force
      
    • Modify resources/views/vendor/toast-magic/themes/ and recompile assets.
  2. Event Listeners:

    • Extend Livewire events by adding a global listener:
      Livewire::listen('toastMagic', function ($event) {
          // Log or modify events
      });
      
  3. Middleware:

    • Disable toasts for specific routes:
      public function handle($request, Closure $next) {
          if (!$request->wantsJson()) {
              app()->singleton(ToastMagic::class, fn() => new class {
                  public function __call($method, $args) { return null; }
              });
          }
          return $next($request);
      }
      
  4. Testing:

    • Mock the facade in PHPUnit:
      $this->mock(ToastMagic::class)->shouldReceive('success')->once();
      
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai