Download the PHP package erikgall/transformer without Composer
On this page you can find all versions of the php package erikgall/transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download erikgall/transformer
More information about erikgall/transformer
Files in erikgall/transformer
Package transformer
Short Description Laravel model transformers with relationships for API based applications.
License MIT
Homepage https://github.com/erikgall/transformer
Informations about the package transformer
Laravel Model Transformer
Transform Laravel Eloquent models into an array of specified keys with the option of including nested relationships as their transformed model array.
The Reason
This package was created to ease complexity, simplify model's, while allowing for them to be quickly transposed into an array of specified keys.
But turning an eloquent model into an array is easy right? You only have to call the toArray()
method. The shortfall comes when you want the final array to only have specific fields/keys, as well as a nested relationship. You end up transforming the model inside of your controller, specifying each and every key and value the array should have.
Example
Without Laravel Model Transformer
With Laravel Model Transformer
And it returns:
Getting Started
These instructions will get you get you up and running and using the package inside of your Laravel application.
Prerequisities
The package was developed using Laravel 5.2 and PHP 7 so there may be incompatibilities with PHP 5.
Installing
To install the package inside of your Laravel app, please follow the steps below.
Step 1: Run the following command from your project root.
Step 2: Add the package's service provider to your provider's array in you app.php
config file.
Step 3: Create a transformers directory inside of the directory where your model files live. Example using the location of a User model and its transformer class:
- app/User.php -> app/transformers/UserTransformer.php
- app/Models/User.php -> app/models/transformers/UserTransformer.php
* The Transformers directory must be in the same directory as you models... Only if you wish to use the automatic transformer class finder. Otherwise you must specify the model's transformer class/full namespace.
Step 4: Any models use wish to use a transformer with must implement the EGALL\Transformer\Contracts\Transformable
interface/contract. You should also include the trait EGALL\Transformer\Traits\TransformableModel
inside of your model classes.
Example below:
User Model Transformer Class
Using the User Model Transformer from a controller.
Running the tests
Install the package and install it's dependencies. After, that is complete and once inside the package's root, run the following command:
Built With/Using
- Laravel
- PHPUnit
- PhpStorm
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
- Erik Galloway - Flip Learning
License
This project is licensed under the MIT License - see the LICENSE.md file for details