Toaster class is the core service that manages toast notifications. It’s bound to the Laravel service container and is accessed through the Toast facade or the toast() helper function.
Class overview
Public methods
success()
Add a success toast notification.The success message to display.
An optional title for the toast.
Duration in milliseconds. If not provided, uses the default from configuration.
Toaster instance for method chaining
Example:
error()
Add an error toast notification.The error message to display.
An optional title for the toast.
Duration in milliseconds. If not provided, uses the default from configuration.
Toaster instance for method chaining
Example:
info()
Add an informational toast notification.The informational message to display.
An optional title for the toast.
Duration in milliseconds. If not provided, uses the default from configuration.
Toaster instance for method chaining
Example:
warning()
Add a warning toast notification.The warning message to display.
An optional title for the toast.
Duration in milliseconds. If not provided, uses the default from configuration.
Toaster instance for method chaining
Example:
add()
Add a toast notification with a specific level.The message to display in the toast notification.
The level/type of the toast. One of:
ToastLevel::Success, ToastLevel::Error, ToastLevel::Info, or ToastLevel::Warning.An optional title for the toast.
Duration in milliseconds. If not provided, uses the default from configuration.
Toaster instance for method chaining
Example:
hasPending()
Check if there are pending toasts that have been added but not yet flashed.bool - true if there are pending toasts, false otherwise
Example:
getPending()
Get all pending toast messages. This method is primarily used for testing.ToastMessage[] - Array of pending toast messages
Example:
getPropKey()
Get the Inertia prop key used for toast notifications.string - The prop key from configuration (default: 'toasts')
Example:
How it works
TheToaster class manages toast notifications through a simple workflow:
- When you call a method like
success(),error(), etc., it creates a newToastMessageinstance - The toast message is added to the internal
$pendingarray - The toasts are immediately flashed to Inertia using
Inertia::flash() - The toasts are available in your frontend Inertia props under the configured prop key
ToastMessage class
Each toast is represented by aToastMessage instance:
Properties
The toast message content.
The toast level (Success, Error, Info, or Warning).
Optional title for the toast.
Optional duration in milliseconds.
toArray()
Convert the toast message to an array format for JSON serialization.message, level, title, duration
Example output:
ToastLevel enum
TheToastLevel enum defines the available toast types:
Method chaining
All toast methods return theToaster instance, allowing for method chaining:
Service container binding
TheToaster class is bound to the Laravel service container as a singleton:
Most developers will use the
Toast facade or toast() helper instead of interacting with the Toaster class directly. However, understanding the underlying class is useful for advanced use cases and testing.Testing
TheToaster class provides methods useful for testing: