Package Data | |
---|---|
Maintainer Username: | cyvelnet |
Maintainer Contact: | cyvelnet@gmail.com (Cyvelnet) |
Package Create Date: | 2015-03-17 |
Package Last Update: | 2024-03-13 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:26:36 |
Package Statistics | |
---|---|
Total Downloads: | 173,546 |
Monthly Downloads: | 980 |
Daily Downloads: | 45 |
Total Stars: | 79 |
Total Watchers: | 6 |
Total Forks: | 21 |
Total Open Issues: | 1 |
A simple fractal service provider and transformer generator for laravel 5 and lumen
Require this package with composer using the following command:
composer require cyvelnet/laravel5-fractal
After updating composer, add the ServiceProvider to the providers array in config/app.php
Cyvelnet\Laravel5Fractal\Laravel5FractalServiceProvider::class,
and register Facade
And optionally add a new line to the aliases
array:
'Fractal' => Cyvelnet\Laravel5Fractal\Facades\Fractal::class
register service provider in /bootstrap/app.php for lumen
$app->register(Cyvelnet\Laravel5Fractal\Laravel5FractalServiceProvider::class);
and uncomment the line
$app->withFacades();
and finally register Facade with
class_alias(Cyvelnet\Laravel5Fractal\Facades\Fractal::class, 'Fractal');
You can also publish the config file to change implementations to suits you.
php artisan vendor:publish --provider="Cyvelnet\Laravel5Fractal\Laravel5FractalServiceProvider"
Auto inject/embed sub resources are disabled by default, to enable this feature, edit config/fractal.php
and set
autoload => true
cyvelnet/fractal
come with a helpful commandline to assist your api transformation, just type and your Eloquent model attributes will be added to your transform array automatically
// generate a empty transformer
php artisan make:transformer UserTransformer
// generate a modeled transformer
php artisan make:transformer UserTransformer -m User
Transform a single record
$user = User::find(1);
Fractal::item($user, new UserTransformer());
Transform a collection of records
$users = User::where('activated', true)->get();
// $resourceKey is optional for most serializer, but recommended to set for JsonApiSerializer
$resourceKey = 'user';
Fractal::collection($users, new UserTransformer(), $resourceKey);
Inject sub resources
Fractal::includes('orders') // where 'orders' is defined in your transformer class's $availableIncludes array
Remove sub resources
Fractal::excludes('orders')
Change transformer serializer
Fractal::setSerializer(\Acme\MySerializer); // where MySerializer is a class extends \League\Fractal\Serializer\SerializerAbstract
add sparse fieldset
Fractal::fieldsets(['orders' => 'item,qty,total,date_order'])
add extra meta data to root
// specify with single meta data
Fractal::addMeta($key = 'metaKey', $data = 'metaData')
// add an array of meta data
Fractal::addMeta([
'key1' => 'data1',
'key2' => 'data2'
])
https://github.com/Cyvelnet/laravel5-fractal/wiki/Transformable-Trait
https://github.com/Cyvelnet/laravel5-fractal/wiki/Custom-TransformerableAbstract-class