> ## Documentation Index
> Fetch the complete documentation index at: https://veekthoven-laravel-inertia-toast-24-61.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# toast() helper

> The toast() helper function provides a fluent interface for creating toast notifications

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

```php theme={null}
function toast(?string $message = null): PendingToast|Toaster
```

<ParamField path="message" type="string | null" default="null">
  The message for the toast notification. If provided, returns a `PendingToast` builder. If omitted, returns the `Toaster` instance.
</ParamField>

**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:

```php theme={null}
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

```php theme={null}
toast('Profile updated')
    ->title('Success')
    ->duration(5000)
    ->success();

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

### Access Toaster instance

```php theme={null}
$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.

```php theme={null}
title(string $title): PendingToast
```

<ParamField path="title" type="string" required>
  The title to display above the toast message.
</ParamField>

**Returns:** `PendingToast` instance for method chaining

**Example:**

```php theme={null}
toast('Your changes have been saved')
    ->title('Success')
    ->success();
```

### duration()

Set how long the toast should be displayed.

```php theme={null}
duration(int $milliseconds): PendingToast
```

<ParamField path="milliseconds" type="int" required>
  The duration in milliseconds to display the toast notification.
</ParamField>

**Returns:** `PendingToast` instance for method chaining

**Example:**

```php theme={null}
toast('This message will show for 10 seconds')
    ->duration(10000)
    ->info();
```

### success()

Commit the toast as a success notification.

```php theme={null}
success(): Toaster
```

**Returns:** `Toaster` instance

**Example:**

```php theme={null}
toast('User registered successfully')
    ->title('Registration Complete')
    ->duration(5000)
    ->success();
```

### error()

Commit the toast as an error notification.

```php theme={null}
error(): Toaster
```

**Returns:** `Toaster` instance

**Example:**

```php theme={null}
toast('Failed to process payment')
    ->title('Payment Error')
    ->duration(8000)
    ->error();
```

### info()

Commit the toast as an informational notification.

```php theme={null}
info(): Toaster
```

**Returns:** `Toaster` instance

**Example:**

```php theme={null}
toast('Check your email for confirmation')
    ->title('Verification Required')
    ->info();
```

### warning()

Commit the toast as a warning notification.

```php theme={null}
warning(): Toaster
```

**Returns:** `Toaster` instance

**Example:**

```php theme={null}
toast('This action cannot be undone')
    ->title('Warning')
    ->duration(6000)
    ->warning();
```

## Complete examples

### Basic usage

```php theme={null}
// Simple success message
toast('Profile updated')->success();

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

### Advanced usage

```php theme={null}
// 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

```php theme={null}
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

```php theme={null}
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

<CodeGroup>
  ```php Helper function theme={null}
  toast('User created')
      ->title('Success')
      ->duration(5000)
      ->success();
  ```

  ```php Toast facade theme={null}
  use InertiaToast\Facades\Toast;

  Toast::success('User created', 'Success', 5000);
  ```
</CodeGroup>

<Note>
  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.
</Note>

## 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.
