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

# Server-side usage

> Trigger toast notifications from your Laravel backend using the Toast facade or toast() helper

You can trigger toast notifications from your Laravel controllers, routes, or any backend code using either the `Toast` facade or the `toast()` helper function.

## Using the facade

The `Toast` facade provides static methods for each toast type:

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

Toast::success('Profile updated!');
Toast::error('Something went wrong.');
Toast::info('Check your email for a confirmation link.');
Toast::warning('Your subscription is about to expire.');

return redirect()->route('dashboard');
```

### Method signatures

Each toast method accepts the following parameters:

```php theme={null}
Toast::success(string $message, ?string $title = null, ?int $duration = null): Toaster
Toast::error(string $message, ?string $title = null, ?int $duration = null): Toaster
Toast::info(string $message, ?string $title = null, ?int $duration = null): Toaster
Toast::warning(string $message, ?string $title = null, ?int $duration = null): Toaster
```

## Using the helper

The `toast()` helper provides two usage patterns:

### Fluent builder pattern

When called with a message, it returns a `PendingToast` instance that allows you to chain configuration methods:

```php theme={null}
toast('Profile updated!')->success();
toast('Something went wrong.')->error();
toast('Slow message.')->duration(10000)->warning();
```

### Direct toaster access

When called without arguments, it returns the `Toaster` instance for direct method calls:

```php theme={null}
toast()->success('Quick shorthand');
toast()->error('Operation failed');
```

## Toast types

Laravel Inertia Toast supports four toast types:

<Tabs>
  <Tab title="Success">
    Use for successful operations:

    ```php theme={null}
    Toast::success('Profile updated successfully!');
    toast('Changes saved!')->success();
    ```
  </Tab>

  <Tab title="Error">
    Use for errors and failures:

    ```php theme={null}
    Toast::error('Failed to process payment.');
    toast('Something went wrong.')->error();
    ```
  </Tab>

  <Tab title="Info">
    Use for informational messages:

    ```php theme={null}
    Toast::info('Check your email for confirmation.');
    toast('New features available!')->info();
    ```
  </Tab>

  <Tab title="Warning">
    Use for warnings:

    ```php theme={null}
    Toast::warning('Your subscription expires soon.');
    toast('Low disk space detected.')->warning();
    ```
  </Tab>
</Tabs>

## Adding a title

You can add an optional title to provide context for your toast messages.

### With the facade

Use named arguments to specify the title:

```php theme={null}
Toast::success('Profile has been updated.', title: 'Success');
Toast::error('Unable to connect to database.', title: 'Connection Error');
```

### With the helper

Chain the `title()` method:

```php theme={null}
toast('Item has been removed.')->title('Deleted')->error();
toast('Operation completed successfully.')->title('Done')->success();
```

## Custom duration

By default, toasts auto-dismiss after the duration specified in your config (5000ms). You can override this per toast.

<Note>
  Duration is specified in milliseconds. For example, `3000` = 3 seconds.
</Note>

### With the facade

```php theme={null}
Toast::success('Saved!', duration: 3000); // 3 seconds
Toast::warning('Session expiring soon.', duration: 10000); // 10 seconds
```

### With the helper

Chain the `duration()` method:

```php theme={null}
toast('Done!')->duration(3000)->success();
toast('Important message')->duration(10000)->info();
```

## Combining title and duration

You can combine both title and duration options:

### With the facade

```php theme={null}
Toast::warning(
    'Your session is about to expire.',
    title: 'Warning',
    duration: 10000
);

Toast::success(
    'Report generated successfully.',
    title: 'Export Complete',
    duration: 8000
);
```

### With the helper

```php theme={null}
toast('Your session is about to expire.')
    ->title('Warning')
    ->duration(10000)
    ->warning();

toast('Report generated successfully.')
    ->title('Export Complete')
    ->duration(8000)
    ->success();
```

## Multiple toasts

You can queue multiple toasts in a single request. They will be displayed according to your `max_visible` configuration:

```php theme={null}
public function update(Request $request)
{
    // Process multiple operations
    $this->updateProfile($request->user());
    $this->syncPreferences($request->user());
    $this->notifyAdmins($request->user());

    Toast::success('Profile updated.');
    Toast::success('Preferences synced.');
    Toast::info('Admins have been notified.');

    return redirect()->route('dashboard');
}
```

## Working with redirects

Toasts work seamlessly with redirects because they're flashed to the session via Inertia:

```php theme={null}
public function store(Request $request)
{
    $post = Post::create($request->validated());

    toast('Post published successfully!')->success();

    return redirect()->route('posts.show', $post);
}
```

<Tip>
  Toasts persist across redirects, so you can trigger them before returning a redirect response.
</Tip>

## Common patterns

Here are some common usage patterns:

<CodeGroup>
  ```php Form validation theme={null}
  public function store(Request $request)
  {
      $validated = $request->validate([
          'email' => 'required|email',
          'password' => 'required|min:8',
      ]);

      User::create($validated);

      Toast::success('Account created successfully!', title: 'Welcome');

      return redirect()->route('dashboard');
  }
  ```

  ```php Error handling theme={null}
  public function process()
  {
      try {
          $this->performOperation();
          toast('Operation completed.')->success();
      } catch (\Exception $e) {
          toast('Operation failed: ' . $e->getMessage())->error();
      }

      return redirect()->back();
  }
  ```

  ```php Conditional toasts theme={null}
  public function publish(Post $post)
  {
      if ($post->isDraft()) {
          $post->publish();
          Toast::success('Post published successfully!');
      } else {
          Toast::warning('Post is already published.');
      }

      return redirect()->route('posts.show', $post);
  }
  ```
</CodeGroup>

## API reference

### PendingToast methods

The `PendingToast` class (returned by `toast('message')`) provides these chainable methods:

* `title(string $title): static` - Set the toast title
* `duration(int $milliseconds): static` - Set custom duration in milliseconds
* `success(): Toaster` - Commit as a success toast
* `error(): Toaster` - Commit as an error toast
* `info(): Toaster` - Commit as an info toast
* `warning(): Toaster` - Commit as a warning toast

### Toaster methods

The `Toaster` class (accessed via `Toast` facade or `toast()` without arguments) provides:

* `success(string $message, ?string $title = null, ?int $duration = null): Toaster`
* `error(string $message, ?string $title = null, ?int $duration = null): Toaster`
* `info(string $message, ?string $title = null, ?int $duration = null): Toaster`
* `warning(string $message, ?string $title = null, ?int $duration = null): Toaster`
* `add(string $message, ToastLevel $level, ?string $title = null, ?int $duration = null): Toaster`
* `hasPending(): bool` - Check if there are pending toasts
* `getPropKey(): string` - Get the Inertia flash key
