Skip to main content

Flash Messages

Flash messages let you show success, info, warning, and error toasts after a redirect.

Usage

  1. Return a redirect response from your controller.
  2. Chain a flash macro on the redirect.
<?php

class ExampleController extends Controller
{
    public function store(Request $request)
    {
        // Your logic here...

        return redirect()->route('dashboard')
            ->withSuccess(__('Record created successfully.'));
    }

    public function update(Request $request)
    {
        // Your logic here...

        return redirect()->back()
            ->withInfo(__('Record updated.'));
    }

    public function someWarning()
    {
        return redirect()->route('settings')
            ->withWarning(__('Please review your settings.'));
    }

    public function handleError()
    {
        return redirect()->back()
            ->withError(__('Something went wrong.'));
    }
}

Available Methods

  • withSuccess(string $message) - Show a green success toast.
  • withInfo(string $message) - Show a blue info toast.
  • withWarning(string $message) - Show a yellow warning toast.
  • withError(string $message) - Show a red error toast and add the error to Laravel’s error bag.

Frontend Behavior

Flash data is shared on every Inertia response and displayed automatically.
  • Shared globally via HandleInertiaRequests.php.
  • Included with Inertia responses, even when using only: [] partial reloads.
  • Available in the useGlobalProps() hook as flash.
  • Displayed as Sonner toasts in guest-layout.tsx and app-layout.tsx.
  • Shown automatically on page load when flash data exists.

Error Bag Integration

Note withError() also adds the message to the general key in Laravel’s error bag, so you can access it via errors.general in React.

How It Works

  1. The redirect macro adds flash data to the session.
  2. HandleInertiaRequests shares flash data with all Inertia responses.
  3. The FlashMessage component reads flash data from global props.
  4. The component triggers a Sonner toast based on the message type.
  5. Messages are cleared after being displayed.