Laravel Fractal

spatie/laravel-fractal image

Laravel Fractal stats

Downloads
8.5M
Stars
1,840
Open Issues
0
Forks
185

View on GitHub →

An easy to use Fractal integration for Laravel applications

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 photo

We create open source, digital products and courses for the developer community

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Laravel Fractal Related Articles

Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Harpoon: Next generation time tracking and invoicing logo

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
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic