Flash Messages
Flash messages let you show success, info, warning, and error toasts after a redirect.Usage
- Return a redirect response from your controller.
- Chain a flash macro on the redirect.
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 asflash. - Displayed as Sonner toasts in
guest-layout.tsxandapp-layout.tsx. - Shown automatically on page load when flash data exists.
Error Bag Integration
NotewithError()also adds the message to thegeneralkey in Laravel’s error bag, so you can access it viaerrors.generalin React.
How It Works
- The redirect macro adds flash data to the session.
HandleInertiaRequestsshares flash data with all Inertia responses.- The
FlashMessagecomponent reads flash data from global props. - The component triggers a Sonner toast based on the message type.
- Messages are cleared after being displayed.