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.string
required
The success message to display.
string | null
default:"null"
An optional title for the toast.
int | null
default:"null"
Duration in milliseconds. If not provided, uses the default from configuration.
Toaster instance for method chaining
Example:
error()
Add an error toast notification.string
required
The error message to display.
string | null
default:"null"
An optional title for the toast.
int | null
default:"null"
Duration in milliseconds. If not provided, uses the default from configuration.
Toaster instance for method chaining
Example:
info()
Add an informational toast notification.string
required
The informational message to display.
string | null
default:"null"
An optional title for the toast.
int | null
default:"null"
Duration in milliseconds. If not provided, uses the default from configuration.
Toaster instance for method chaining
Example:
warning()
Add a warning toast notification.string
required
The warning message to display.
string | null
default:"null"
An optional title for the toast.
int | null
default:"null"
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.string
required
The message to display in the toast notification.
ToastLevel
default:"ToastLevel::Info"
The level/type of the toast. One of:
ToastLevel::Success, ToastLevel::Error, ToastLevel::Info, or ToastLevel::Warning.string | null
default:"null"
An optional title for the toast.
int | null
default:"null"
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
string
required
The toast message content.
ToastLevel
default:"ToastLevel::Info"
The toast level (Success, Error, Info, or Warning).
string | null
default:"null"
Optional title for the toast.
int | null
default:"null"
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: