Skip to content

Instantly share code, notes, and snippets.

@zackify
Last active February 8, 2017 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackify/7fe0603a22515d014f7e3f43e2dac373 to your computer and use it in GitHub Desktop.
Save zackify/7fe0603a22515d014f7e3f43e2dac373 to your computer and use it in GitHub Desktop.
public function __construct(Book $bookModel, BookTransformer $transformer)
{
parent::__construct();
$this->transmit = [
'transformer' => $transformer,
'model' => $bookModel,
];
}
/**
* Show a list of Books.
*
* @param Request $request
* @return mixed
*/
public function index(Request $request)
{
return $this->respondWithPaginatedCollection();
//defaults to this->transmit->model as the $optionalModelItem. The transformer is already set too in constructor
return $this->respondWithPaginatedCollection($optionalModelItem);
/* inside your respond with methods they will run
$includes = $this->transmit->transformer->getEagerLoads($this->fractal->getRequestedIncludes());
$items = $this->eagerLoadIncludes($this->transmit->model, $includes);
$items = $this->applyParameters($items, request()->query);
*/
}
@joshforbes
Copy link

One thing that might be a problem. In some controllers I end up using two transformers. For example: the Job controller. In the index and show methods I use the JobTransformer but in the store and update methods I use the JobBuilderTransformer.

Those could (and maybe should) be split into two different controllers: JobController and JobBuilderController. So maybe it is not something to worry about.

Maybe this even enforces keeping controllers focused on one task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment