PHP code example of cyvelnet / laravel5-fractal

1. Go to this page and download the library: Download cyvelnet/laravel5-fractal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cyvelnet / laravel5-fractal example snippets


Cyvelnet\Laravel5Fractal\Laravel5FractalServiceProvider::class,
    
$app->register(Cyvelnet\Laravel5Fractal\Laravel5FractalServiceProvider::class);

$app->withFacades();

class_alias(Cyvelnet\Laravel5Fractal\Facades\Fractal::class, 'Fractal');
 

$user = User::find(1);

Fractal::item($user, new UserTransformer());

 

$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);

 

Fractal::
 

Fractal::excludes('orders')

 

Fractal::setSerializer(\Acme\MySerializer); // where MySerializer is a class extends \League\Fractal\Serializer\SerializerAbstract 

 

Fractal::fieldsets(['orders' => 'item,qty,total,date_order'])
 

// specify with single meta data
Fractal::addMeta($key = 'metaKey', $data = 'metaData')

// add an array of meta data
Fractal::addMeta([
    'key1' => 'data1',
    'key2' => 'data2'
    ])

`bash
php artisan vendor:publish --provider="Cyvelnet\Laravel5Fractal\Laravel5FractalServiceProvider"