Package Data | |
---|---|
Maintainer Username: | sameeranand1 |
Maintainer Contact: | sanand@cs.stonybrook.edu (Sameer Anand) |
Package Create Date: | 2017-04-22 |
Package Last Update: | 2017-04-23 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-01-14 03:00:18 |
Package Statistics | |
---|---|
Total Downloads: | 20 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
This package helps developers to easily transform data in a item or in a collection of items.
Install package through Composer:
composer require spawned/transformer
Register service provider in the providers array in config/app.php
Spawned\Transformer\TransformerServiceProvider::class
You can generate a new transformer by running php artisan make:transformer {name}
. For example:
php artisan make:transformer UserTransformer
This will create app\Transformers\UserTransformer.php
A generated transformer will look like this:
class UserTransformer extends Transformer
{
public function transform($user)
{
return [
'name' => $user->first . ' ' . $user->last
];
}
}
Modify the returned array in the transform method to define how you want the data to be transformed.
You can transform a single item using the transform method. For example:
public function show(User $user, UserTransformer $transformer)
{
return $transformer->transform($user);
}
You can transform a collection of items using the transformCollection method. For example:
public function index(UserTransformer $transformer)
{
$users = User::all();
return $transformer->transformCollection($users);
}
Released under the MIT License, Copyright (c) 2017 - Sameer Anand (sanand@cs.stonybrook.edu)