Last week v2 of laravel-fractal was released. This package is a developer friendly wrapper around the League's Fractal package. It a Laravel context it can be used to transform your Eloquent models to JSON output for an API. Think of it as toJson (or toArray) on steroids.

This is how you can work with the league's package:

use League\Fractal\Manager;
use League\Fractal\Resource\Collection;

$books = [
   ['id'=>1, 'title'=>'Hogfather', 'characters' => [...]], 
   ['id'=>2, 'title'=>'Game Of Kill Everyone', 'characters' => [...]]
];

$manager = new Manager();

$resource = new Collection($books, new BookTransformer());

$manager->createData($resource)->toArray();

Laravel-fractal makes that a lot easier:

fractal()
   ->collection($books)
   ->transformWith(new BookTransformer())
   ->toArray();

With the newly tagged v2 of our package you can compact that code to this:

fractal($books, new BookTransformer())->toArray();

You can even pass in a callable as a second argument.

fractal($books, function(Book $book) {
   // do your transforming here
})->toArray();

Fractal and laravel-fractal can do a whole lot more. Head over to the docs on GitHub to learn all the options.

If you were already using v1 of the package, it's fairly easy to upgrade to v2.