---
title: "Serializing for an API — Onboarding for Laravel | Roundly"
description: "Hand a flow to your frontend as a JSON-ready array or as typed FlowData, StepData and SectionData DTOs."
url: https://roundly-consulting.com/open-source/docs/onboarding-for-laravel/serialization
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Onboarding for Laravel](https://roundly-consulting.com/open-source/docs/onboarding-for-laravel.md)

# Serializing for an API

Flow::toArray() produces a structure ready to hand to a frontend:

```php
[
    'title' => 'Profile Onboarding',
    'percentage' => 50.0,
    'next_step' => [
        'key' => 'upload-photo',
        'title' => 'Upload Photo',
        'cta' => 'Upload Now',
        'action' => 'users@photo@upload',
        'is_completed' => false,
        'is_optional' => false,
        'meta' => [],
        'group' => null,
    ],
    'current_step' => [
        // same shape as next_step
    ],
    'is_completed' => false,
    'steps' => [
        // each step as ['key', 'title', 'cta', 'action', 'is_completed', 'is_optional', 'meta', 'group']
    ],
]
```

next\_step and current\_step carry the same step; both are null once no visible step is left incomplete. Titles and CTAs are the resolved copy.

## Typed DTOs

For a typed equivalent, Flow::toData() returns a FlowData DTO and Step::toData() a StepData. Every DTO is final readonly and implements Arrayable and JsonSerializable:

```php
use RoundlyConsulting\Onboarding\DataTransferObjects\FlowData;

$data = Onboarding::for($user)?->toData();   // ?FlowData

$data?->title;          // ?string
$data?->percentage;     // float
$data?->currentStep;    // ?StepData — the same step as $data->nextStep
$data?->steps;          // list<StepData>
$data?->isCompleted;    // bool

$data?->currentStep?->key;      // 'upload-photo'
$data?->currentStep?->title;    // resolved title
```

| DTO | Properties | Source |
| --- | --- | --- |
| `FlowData` | `title, percentage, nextStep, currentStep, steps, isCompleted` | Returned by Flow::toData(). |
| `StepData` | `key, title, cta, action, isCompleted, isOptional, meta, group` | Returned by Step::toData(); title and cta are resolved. |
| `SectionData` | `key, title, percentage, isCompleted, steps` | Returned by sections(), groups() and section(). |

## In an API route

```php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use RoundlyConsulting\Onboarding\Facades\Onboarding;

Route::middleware('auth')->get('/api/onboarding', function (Request $request) {
    return response()->json(Onboarding::for($request->user())?->toData());
});
```

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/onboarding-for-laravel.md)
