Package

Spatie Laravel Fractal

An easy to use Fractal integration for Laravel applications

Package

spatie/laravel-fractal

View on GitHub →

Spatie Laravel Fractal Package

The Spatie Laravel Fractal package provides a simplified wrapper for the Fractal library, which is designed to handle complex data transformations in API development. It integrates seamlessly into Laravel and Lumen applications, making the process of transforming data models into arrays or JSON output more intuitive and efficient.

Key Features

  • Simplified Syntax: Streamlines the process of transforming data with a concise and easy-to-use syntax.
  • Facade Support: Includes a facade for traditional Laravel style coding.
  • Integration with Eloquent: Directly transform data retrieved from Eloquent models.
  • Auto Includes: Automatically handle API resource inclusion based on request parameters.
  • Customizable: Publish and customize configuration settings like default serializer, paginator, or even the base Fractal class.
  • Quick Transformations: Facilitates quick transformations with minimal code, ideal for rapid API development.

Installation

For Laravel 5.5 and newer:

composer require spatie/laravel-fractal

The package will auto-register. To publish and modify the configuration:

php artisan vendor:publish --provider="Spatie\Fractal\FractalServiceProvider"

Usage

Transforming data is straightforward:

$books = fractal($books, new BookTransformer())->toArray();

To return a JSON response in a Laravel application:

return fractal($books, new BookTransformer())->respond();

Additional response customization options include setting status codes, headers, and JSON encoding options.

Extending Functionality

The package allows for macroable extensions to the Fractal class, enabling developers to add custom methods without cluttering the primary codebase.

Development and Support

  • Running Tests: Execute composer test to run the test suite.
  • Contributions: Contributions are welcome, and the guidelines are listed in the CONTRIBUTING document.
  • Security Issues: Direct security-related concerns to Spatie via their provided contact email.

For a comprehensive understanding of all available methods and further examples, developers should refer to the documentation of spatie/fractalistic.

This package streamlines working with complex data transformations in Laravel, enhancing API development with its elegant approach to managing resource output.

Spatie Laravel Fractal Related Articles

Lumen and Fractal image

Lumen and Fractal

Read article
Media Library Pro: Vue, React and Blade components to handle uploads (sponsor) image

Media Library Pro: Vue, React and Blade components to handle uploads (sponsor)

Read article
Spatie Shares Their Coding Guidelines as AI Skills image

Spatie Shares Their Coding Guidelines as AI Skills

Read article
Image Optimization With Spatie Laravel Image Optimizer image

Image Optimization With Spatie Laravel Image Optimizer

Read article
Driver-Based Architecture in Spatie's Laravel PDF v2 image

Driver-Based Architecture in Spatie's Laravel PDF v2

Read article
What is the one package you install in all Laravel projects? image

What is the one package you install in all Laravel projects?

Read article

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing