2017 © Pedro Peláez
 

library fractal-helpers

image

rdarcy1/fractal-helpers

  • Wednesday, June 6, 2018
  • by rdarcy1
  • Repository
  • 1 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Fractal Helpers

A small extension for Fractal that makes it easy to include relationships on a resource. Designed for use with Laravel/Eloquent, but it will work with any model or resource that expresses the same interface for retrieving relationships., (*1)

Installation

composer require konsulting/fractal-helpers

Usage

To make use of this package, in your transformers extend Konsulting\FractalHelpers\TransformerAbstract rather than the base Fractal transformer., (*2)

When building up APIs with Fractal I found myself repeating the same code to include relations on my Eloquent models:, (*3)

// BookTransformer.php

protected $availableIncludes = [
    'author',
    'characters'
];

public function includeAuthor(Book $book) {
     return $this->item($book->author, new AuthorTransformer);
}

public function includeCharacters(Book $book) {
    return $this->collection($book->characters, new CharacterTransformer);
}

With the included TransformerAbstract class, you can express the above code more succinctly as:, (*4)

// BookTransformer.php

protected $itemIncludes = [
    'author' => AuthorTransformer::class,
];

protected $collectionIncludes = [
    'characters' => CharacterTransformer::class,
];

Null relationships

If a relationship returns null, it will automatically be converted to a League\Fractal\Resource\NullResource object rather than passing to the associated transformer for that relationship. This means it's not necessary to check for null within each transformer., (*5)

The Versions