Skip to content

Latest commit

 

History

History
65 lines (50 loc) · 1.38 KB

File metadata and controls

65 lines (50 loc) · 1.38 KB

Fluent Transition API

StateFlow provides a fluent API for building transitions with full control over the process.

Basic Usage

$result = $order->transition()
    ->to(Shipped::class)
    ->execute();

if ($result->succeeded()) {
    // Transition completed
}

With Metadata

$result = $order->transition()
    ->to(Shipped::class)
    ->reason('Order shipped via FedEx')
    ->metadata([
        'tracking_number' => 'FX123456789',
        'carrier' => 'FedEx',
        'estimated_delivery' => '2026-01-15',
    ])
    ->execute();

Full Example

$result = $order->transition()
    ->to(Shipped::class)
    ->reason('Shipped from warehouse')
    ->metadata(['tracking' => $trackingNumber])
    ->performer($currentUser)     // Specify who performs the transition
    ->execute();

if ($result->failed()) {
    return back()->with('error', $result->error);
}

return back()->with('success', 'Order shipped successfully!');

Chaining Options

$order->transition()
    ->to(Processing::class)
    ->reason('Bulk processing')
    ->silent()              // Don't fire events
    ->execute();

Benefits

  • Clean, readable code
  • All transition options in one place
  • Easy to extend with additional options
  • IDE-friendly with autocompletion

📦 See it in action: laravel-stateflow-demo