2017 © Pedro Peláez
 

library laravel-fractal

Fractal helpers for Laravel 5

image

vice/laravel-fractal

Fractal helpers for Laravel 5

  • Wednesday, July 13, 2016
  • by maxbrokman
  • Repository
  • 3 Watchers
  • 1 Stars
  • 1,217 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 6 % Grown

The README.md

Another Fractal Service Provider for Laravel 5 and Lumen

Latest Version on Packagist Build Status Software License, (*1)

Fractal lets you present API data in a consistent way, by acting as an anti-corruption layer between your frontend and backend., (*2)

Read up on Fractal here., (*3)

Installation

Require this package, (*4)

composer require vice/laravel-fractal

And then add the following to the service providers in app.php, (*5)

Vice\LaravelFractal\FractalServiceProvider::class,

Usage

To send a JSON representation of a single entity to the frontend simply, (*6)

public function show($id)
{
    //...

    fractalResponse()->item($thing, new ThingTransformer);
}

To send a JSON representation of a collection of entities to the frontend simply, (*7)

public function index()
{
    //...

    fractalResponse()->collection($things, new ThingTransformer);
}

The collection method also supports paginators, and will automatically append their state under a meta key, (*8)

If you need to transform data without immediately using it in a response you may inject Vice\LaravelFractal\FractalService into your controller / class and use it as so:, (*9)

$json = $this->fractalService->item($thing, new ThingTransformer)->toJson();

Contributing

Please open any issues or pull requests on GitHub. This package is maintained by max.brokman@vice.com, (*10)

For PRs please run the style fixer (vendor/bin/php-cs-fixer fix) before submitting., (*11)

The Versions