Skip to main content
The 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.
Returns: The 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.
Returns: The 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.
Returns: The 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.
Returns: The 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.
Returns: The Toaster instance for method chaining Example:

hasPending()

Check if there are pending toasts that have been added but not yet flashed.
Returns: bool - true if there are pending toasts, false otherwise Example:

getPending()

Get all pending toast messages. This method is primarily used for testing.
Returns: ToastMessage[] - Array of pending toast messages Example:

getPropKey()

Get the Inertia prop key used for toast notifications.
Returns: string - The prop key from configuration (default: 'toasts') Example:

How it works

The Toaster class manages toast notifications through a simple workflow:
  1. When you call a method like success(), error(), etc., it creates a new ToastMessage instance
  2. The toast message is added to the internal $pending array
  3. The toasts are immediately flashed to Inertia using Inertia::flash()
  4. The toasts are available in your frontend Inertia props under the configured prop key

ToastMessage class

Each toast is represented by a ToastMessage 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.
Returns: Array with keys: message, level, title, duration Example output:

ToastLevel enum

The ToastLevel enum defines the available toast types:

Method chaining

All toast methods return the Toaster instance, allowing for method chaining:

Service container binding

The Toaster 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

The Toaster class provides methods useful for testing: