Download the PHP package themsaid/laravel-model-transformers without Composer
On this page you can find all versions of the php package themsaid/laravel-model-transformers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download themsaid/laravel-model-transformers
More information about themsaid/laravel-model-transformers
Files in themsaid/laravel-model-transformers
Package laravel-model-transformers
Short Description Easy transformation layer for complex model attributes.
License MIT
Homepage https://github.com/themsaid/laravel-model-transformers
Informations about the package laravel-model-transformers
Laravel 5 Model Transformers
This package helps API developers to easily transform Eloquent models into arrays that are convertible to JSON.
Here's how you use it, let's say you have a model with the following data:
Here you use a numerical value to represent the different types, you also have a mutator in the model that maps the numerical value to a string.
Inside the controller we can transform the model to be represented into more API friendly structure.
The above code will result a JSON string that may look like this:
Installation
Begin by installing the package through Composer. Run the following command in your terminal:
Once composer is done, add the package service provider in the providers array in config/app.php
Finally publish the config file:
That's all what we need.
Usage
Create a model transformer class by extending the AbstractTransformer class:
Now you can call the transformer from any controller:
You can also pass a collection and the result will be an array of transformed models:
Dealing with relationships
The package contains two helpful methods for dealing with relationships, the first one helps you know if a specific relation is eager-loaded:
isRelationshipLoaded()
Now only if the tags are eager-loaded they will be presented in the $output array, this helps reminding you to eager-load when querying models with relationships.
isLoadedFromPivotTable()
This method helps you know if the model is loaded from a ManyToMany relationship, it's helpful when there are pivot data in the table and you would like to present them, example for that:
Passing options to the transformer
You may need to pass some options from the controller to the transformer, you can do that by providing an array of options to the transform()
method as a second parameter:
Now from inside the CategoryTransformer you can check the options parameter:
Using the shorthand method
This package is shipped with a shorthand method for applying transformation for a Model or a Collection:
Using the transform()
method, the package locates the suitable transformer based on the Model or the Collection passed as the first argument.
By default it assumes that all transformers are located under the App\Transformers
namespace, you can change this behaviour in the config file.
You may also pass options to the transformer as a second argument: