2017 © Pedro Peláez
 

library laravel-model-transformer

A simple model transformer compatible with Laravel 5+.

image

itsdamien/laravel-model-transformer

A simple model transformer compatible with Laravel 5+.

  • Monday, May 28, 2018
  • by itsDamien
  • Repository
  • 1 Watchers
  • 2 Stars
  • 4,252 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 1 % Grown

The README.md

itsDamien Laravel Model Transformer, (*1)

Latest Stable Version Total Downloads License Build Status Maintainability Test Coverage StyleCI, (*2)

This package helps API developers to easily transform Eloquent models into collection that are convertible to JSON., (*3)

Installation

Installation using composer:, (*4)

composer require itsdamien/laravel-model-transformer

Usage

Create a model transformer class by extending the AbstractTransformer class:, (*5)

class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer
{
    public function model($model)
    {
        return [
            'first_name' => $model->first_name,
            'last_name'  => $model->last_name,
            'full_name'  => $model->first_name.' '.$model->last_name,
            'photos'     => PhotoTransformer::transform($model->photos),
        ];
    }
}

Now you can call the transformer from any controller:, (*6)

return response([
    "user" => UserTransformer::transform(User::find(1))
]);

// Output:
// {
//     "user":{
//         "first_name":"John",
//         "last_name":"Doe",
//         "full_name":"John Doe",
//         "photos":[]
//     }
// }

You can also pass a collection and the result will be an collection of transformed models:, (*7)

return response([
    "users" => UserTransformer::transform(User::all())
]);

// Output:
// {
//     "users":[
//         {
//             "first_name":"John",
//             "last_name":"Doe",
//             "full_name":"John Doe",
//             "photos":[]
//         },
//         {
//             "first_name":"Dolores",
//             "last_name":"Abernathy",
//             "full_name":"Dolores Abernathy",
//             "photos":[]
//         },
//     ]
// }

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:, (*8)

UserTransformer::transform($user, ['foo' => 'bar']);

Now from inside the UserTransformer you can check the options parameter:, (*9)

class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer
{
    public function model($model)
    {
        return [
            'first_name' => $model->first_name,
            'last_name'  => $model->last_name,
            'full_name'  => $model->first_name.' '.$model->last_name,
            'foo'        => $this->options['foo'],
        ];
    }
}

Complex transformer

Your transformer will always transform your model with the model method. Then you can alter the transformer by adding your with or without methods to the transform() method as a third parameter:, (*10)

class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer
{
    public function model($model)
    {
        return collect([
            'first_name' => $model->first_name,
            'last_name'  => $model->last_name,
            'full_name'  => $model->first_name.' '.$model->last_name,
        ]);
    }

    public function withId($model, \Illuminate\Support\Collection $collection)
    {
        return $collection->merge(collect([
            'id' => $model->id,
        ]));
    }

    public function withoutFullname($model, \Illuminate\Support\Collection $collection)
    {
        return $collection->except('full_name');
    }
}

Now call the transformer:, (*11)

return UserTransformer::transform(User::find(1));

// Output:
// {
//     "first_name":"John",
//     "last_name":"Doe",
//     "full_name":"John Doe"
// }
return UserTransformer::transform(User::find(1), [], ['withId']);

// Output:
// {
//     "id":1,
//     "first_name":"John",
//     "last_name":"Doe",
//     "full_name":"John Doe"
// }
return UserTransformer::transform(User::find(1), [], ['withoutFullname']);

// Output:
// {
//     "first_name":"John",
//     "last_name":"Doe",
// }
return UserTransformer::transform(User::find(1), [], ['withId', 'withoutFullname']);

// Output:
// {
//     "id":1,
//     "first_name":"John",
//     "last_name":"Doe",
// }

The Versions

28/05 2018

dev-master

9999999-dev

A simple model transformer compatible with Laravel 5+.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Damien Criado

laravel api json model

02/12 2017

v2.0.4

2.0.4.0

A simple model transformer compatible with Laravel 5+.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Damien Criado

laravel api json model

08/11 2017

v2.0.3

2.0.3.0

A simple model transformer compatible with Laravel 5+.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Damien Criado

laravel api json model

08/11 2017

v2.0.2

2.0.2.0

A simple model transformer compatible with Laravel 5+.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Damien Criado

laravel api json model

08/11 2017

v2.0.1

2.0.1.0

A simple model transformer compatible with Laravel 5+.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Damien Criado

laravel api json model

08/11 2017

v2.0

2.0.0.0

A simple model transformer compatible with Laravel 5+.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Damien Criado

laravel api json model

06/03 2017

v1.0

1.0.0.0

A simple model transformer compatible with Laravel 5+.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Damien Criado

laravel api json model