Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend TransformerAbstract class to easily include relations in transformers #159

Closed
wants to merge 3 commits into from

Conversation

rdarcy1
Copy link

@rdarcy1 rdarcy1 commented Nov 26, 2017

When building up APIs with fractal I found myself repeating the same code to include relations on my Eloquent models:

// 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);
}

This PR enables you to express the above code more succinctly as:

// BookTransformer.php

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

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

Tests and docs included.

@freekmurze
Copy link
Member

Thanks you for this, but I'm not going to pull it in.

In my mind the added functionality is not worth the added complexity this would bring to the package.

@freekmurze freekmurze closed this Nov 26, 2017
@rdarcy1
Copy link
Author

rdarcy1 commented Nov 27, 2017

Fair enough, thanks for the great package!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants