spatie/fractalistic
Developer-friendly wrapper around League Fractal to transform API data with less boilerplate. Build resources, apply transformers, includes, and output arrays with a fluent API or one-liner syntax. Great for PHP projects; Laravel users may prefer laravel-fractal.
Start by installing the package via Composer: composer require spatie/fractalistic. Import Spatie\Fractalistic\Fractal and begin transforming data with the fluent API. The simplest use case is transforming a simple array:
Fractal::create($books, new BookTransformer())->toArray();
For quick inline transformations, use a closure:
Fractal::create($books, fn($book) => ['id' => $book['id']])->toArray();
If using Laravel, prefer laravel-fractal instead — this package targets non-Laravel or lightweight use cases.
Transformer::class), or closures to transformWith() or directly to collection()/item().->includeCharacters() and ->excludePublisher() for clean, readable relationship inclusion — or fall back to ->parseIncludes(['characters']) for dynamic cases.->serializeWith(new ArraySerializer()) to remove the default data wrapper; configure default serializer globally via config if needed.IlluminatePaginatorAdapter, Cursor) for API pagination metadata — especially critical for JsonApiSerializer.Fractal::create($data, $transformer, $serializer)->toJson() covers most simple cases.laravel-fractal, which builds on this package.->limitRecursion(5) to prevent infinite loops in deeply nested includes — especially when transforming Eloquent relations with bi-directional loading.data, use ->withResourceName('books') (v2.11+) to customize the key — not all serializers respect this, e.g., ArraySerializer ignores wrapper keys.ArraySerializer flattens output ([['id'=>1]] instead of ['data' => [['id'=>1]]]), but JsonApiSerializer expects proper pagination/cursor setup to emit links and meta.Fractal::create()->null() or set null() resource before transformation to avoid errors.How can I help you explore Laravel packages today?