vice / laravel-fractal
Fractal helpers for Laravel 5
Installs: 1 475
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 1
Open Issues: 1
Requires
- illuminate/container: 5.*
- illuminate/http: 5.*
- illuminate/pagination: 5.*
- illuminate/support: 5.*
- league/fractal: ^0.13.0
Requires (Dev)
- friendsofphp/php-cs-fixer: 1.*
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^5.4
This package is not auto-updated.
Last update: 2024-11-09 20:17:37 UTC
README
Fractal lets you present API data in a consistent way, by acting as an anti-corruption layer between your frontend and backend.
Installation
Require this package
composer require vice/laravel-fractal
And then add the following to the service providers in app.php
Vice\LaravelFractal\FractalServiceProvider::class,
Usage
To send a JSON representation of a single entity to the frontend simply
public function show($id) { //... fractalResponse()->item($thing, new ThingTransformer); }
To send a JSON representation of a collection of entities to the frontend simply
public function index() { //... fractalResponse()->collection($things, new ThingTransformer); }
The collection method also supports paginators, and will automatically append their state under a meta
key
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:
$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
For PRs please run the style fixer (vendor/bin/php-cs-fixer fix) before submitting.