Skip to main content
The toast() helper function provides a convenient fluent interface for creating toast notifications. It returns a PendingToast builder when called with a message, or the Toaster instance when called without arguments.

Function signature

function toast(?string $message = null): PendingToast|Toaster
message
string | null
default:"null"
The message for the toast notification. If provided, returns a PendingToast builder. If omitted, returns the Toaster instance.
Returns:
  • PendingToast when a message is provided
  • Toaster when called without arguments

Usage patterns

Fluent builder pattern

The recommended way to use the helper is with the fluent builder pattern:
toast('User created successfully')->success();
toast('An error occurred')->error();
toast('This is a notification')->info();
toast('Please be careful')->warning();

With title and duration

toast('Profile updated')
    ->title('Success')
    ->duration(5000)
    ->success();

toast('Connection failed')
    ->title('Network Error')
    ->duration(7000)
    ->error();

Access Toaster instance

$toaster = toast();
$toaster->success('Operation complete');

PendingToast builder methods

When toast() is called with a message, it returns a PendingToast instance with the following fluent methods:

title()

Set a title for the toast notification.
title(string $title): PendingToast
title
string
required
The title to display above the toast message.
Returns: PendingToast instance for method chaining Example:
toast('Your changes have been saved')
    ->title('Success')
    ->success();

duration()

Set how long the toast should be displayed.
duration(int $milliseconds): PendingToast
milliseconds
int
required
The duration in milliseconds to display the toast notification.
Returns: PendingToast instance for method chaining Example:
toast('This message will show for 10 seconds')
    ->duration(10000)
    ->info();

success()

Commit the toast as a success notification.
success(): Toaster
Returns: Toaster instance Example:
toast('User registered successfully')
    ->title('Registration Complete')
    ->duration(5000)
    ->success();

error()

Commit the toast as an error notification.
error(): Toaster
Returns: Toaster instance Example:
toast('Failed to process payment')
    ->title('Payment Error')
    ->duration(8000)
    ->error();

info()

Commit the toast as an informational notification.
info(): Toaster
Returns: Toaster instance Example:
toast('Check your email for confirmation')
    ->title('Verification Required')
    ->info();

warning()

Commit the toast as a warning notification.
warning(): Toaster
Returns: Toaster instance Example:
toast('This action cannot be undone')
    ->title('Warning')
    ->duration(6000)
    ->warning();

Complete examples

Basic usage

// Simple success message
toast('Profile updated')->success();

// Error with title
toast('Validation failed')
    ->title('Error')
    ->error();

Advanced usage

// Full configuration
toast('Your session will expire in 5 minutes')
    ->title('Session Warning')
    ->duration(10000)
    ->warning();

// Multiple toasts
toast('User deleted')->success();
toast('Related data archived')->info();

In controllers

class UserController extends Controller
{
    public function store(Request $request)
    {
        $user = User::create($request->validated());
        
        toast('User created successfully')
            ->title('Success')
            ->success();
        
        return redirect()->route('users.index');
    }
    
    public function destroy(User $user)
    {
        try {
            $user->delete();
            
            toast('User deleted')->success();
        } catch (\Exception $e) {
            toast('Failed to delete user')
                ->title('Error')
                ->error();
        }
        
        return redirect()->route('users.index');
    }
}

In form requests

class StoreUserRequest extends FormRequest
{
    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            if ($this->somethingElseIsInvalid()) {
                toast('Additional validation failed')
                    ->title('Validation Error')
                    ->error();
            }
        });
    }
}

Comparison with facade

toast('User created')
    ->title('Success')
    ->duration(5000)
    ->success();
The helper function provides a more readable fluent interface, while the facade offers a more concise API. Both approaches are equally valid and produce the same result.

When to use the helper

The toast() helper is ideal when:
  • You want a fluent, readable API
  • You’re building toast configuration step by step
  • You prefer not importing the facade in every file
  • You want IDE autocompletion for the builder pattern
For simpler use cases, the Toast facade may be more concise.