PHPackages                             itsdamien/laravel-model-transformer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. itsdamien/laravel-model-transformer

AbandonedArchivedLibrary[API Development](/categories/api)

itsdamien/laravel-model-transformer
===================================

A simple model transformer compatible with Laravel 5+.

v2.0.6(6y ago)279.9k↑124%MITPHPPHP &gt;=5.6.4

Since Mar 6Pushed 6y ago1 watchersCompare

[ Source](https://github.com/damiencriado/laravel-model-transformer)[ Packagist](https://packagist.org/packages/itsdamien/laravel-model-transformer)[ RSS](/packages/itsdamien-laravel-model-transformer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)DependenciesVersions (8)Used By (0)

[![itsDamien Laravel Model Transformer](https://camo.githubusercontent.com/0e3d621a79e4e9840d9e9db444233856b4e38a376c9c81da489046c80c2ff151/68747470733a2f2f6f686d7962616467652e636f6d2f6f686d7962616467652e7376673f613d69747344616d69656e26623d4c61726176656c2532304d6f64656c2532305472616e73666f726d657226723d3326733d726173706265727279)](https://github.com/itsDamien/laravel-model-transformer)

[![Latest Stable Version](https://camo.githubusercontent.com/aa15ce575627acb93ff117857c19fe0268e4d46a0524fa90728f3384a80973a2/68747470733a2f2f706f7365722e707567782e6f72672f69747364616d69656e2f6c61726176656c2d6d6f64656c2d7472616e73666f726d65722f762f737461626c65)](https://packagist.org/packages/itsdamien/laravel-model-transformer)[![Total Downloads](https://camo.githubusercontent.com/1a79efd7ec657ece36883445c3f2e7367dc85a96a08e74ab8541042cc40de0b0/68747470733a2f2f706f7365722e707567782e6f72672f69747364616d69656e2f6c61726176656c2d6d6f64656c2d7472616e73666f726d65722f646f776e6c6f616473)](https://packagist.org/packages/itsdamien/laravel-model-transformer)[![License](https://camo.githubusercontent.com/7f3cc07355d200653ddad087ac4c7e0b5abe3b37c55aeb0e4867d046d6baab09/68747470733a2f2f706f7365722e707567782e6f72672f69747364616d69656e2f6c61726176656c2d6d6f64656c2d7472616e73666f726d65722f6c6963656e7365)](https://packagist.org/packages/itsdamien/laravel-model-transformer)[![Build Status](https://camo.githubusercontent.com/18e308e1f80263a568dd1d9b5dd06b3aa09789ddc4ffdd2c4e68a9928d6fb5da/68747470733a2f2f7472617669732d63692e6f72672f69747344616d69656e2f6c61726176656c2d6d6f64656c2d7472616e73666f726d65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/itsDamien/laravel-model-transformer)[![Maintainability](https://camo.githubusercontent.com/b9154e1868572ca54eebe9c3ee3f2ce7008492a9606f66faa67a5ed2257f6a1c/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39333461616563323663333339666234366265322f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/itsDamien/laravel-model-transformer/maintainability)[![Test Coverage](https://camo.githubusercontent.com/784d31f60d650c3ba7b6eb5601f0c2ada8d0f325f0fea8d85f781b6f9871d94d/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39333461616563323663333339666234366265322f746573745f636f766572616765)](https://codeclimate.com/github/itsDamien/laravel-model-transformer/test_coverage)[![StyleCI](https://camo.githubusercontent.com/38b97b1372bd5d0364e060d530b29cf7243a6ac60428bd9e3ab7c950ed8f0b84/68747470733a2f2f7374796c6563692e696f2f7265706f732f38333435353331392f736869656c643f6272616e63683d6d6173746572267374796c653d666c6174)](https://styleci.io/repos/83455319)

This package helps API developers to easily transform Eloquent models into collection that are convertible to JSON.

Installation
------------

[](#installation)

Installation using composer:

```
composer require itsdamien/laravel-model-transformer

```

Usage
-----

[](#usage)

Create a model transformer class by extending the `AbstractTransformer` class:

```
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:

```
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:

```
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
----------------------------------

[](#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:

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

Now from inside the `UserTransformer` you can check the options parameter:

```
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
-------------------

[](#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:

```
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:

```
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",
// }
```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~172 days

Recently: every ~197 days

Total

7

Last Release

2324d ago

Major Versions

v1.0 → v2.02017-11-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/82691386c8503584a388af7b6c1f716e07518fb2764ccb036c5c4b341f1dd26c?d=identicon)[damiencriado](/maintainers/damiencriado)

---

Top Contributors

[![damiencriado](https://avatars.githubusercontent.com/u/3238220?v=4)](https://github.com/damiencriado "damiencriado (2 commits)")

---

Tags

apijsonlaravelmodeljsonapilaravelmodel

### Embed Badge

![Health badge](/badges/itsdamien-laravel-model-transformer/health.svg)

```
[![Health](https://phpackages.com/badges/itsdamien-laravel-model-transformer/health.svg)](https://phpackages.com/packages/itsdamien-laravel-model-transformer)
```

###  Alternatives

[aimeos/aimeos-laravel

Cloud native, API first Laravel eCommerce package with integrated AI for ultra-fast online shops, marketplaces and complex B2B projects

8.6k214.7k3](/packages/aimeos-aimeos-laravel)[cloudcreativity/laravel-json-api

JSON API (jsonapi.org) support for Laravel applications.

7881.1M5](/packages/cloudcreativity-laravel-json-api)[joskolenberg/laravel-jory

Create a flexible API for your Laravel application using json based queries.

4513.5k](/packages/joskolenberg-laravel-jory)[wayofdev/laravel-symfony-serializer

📦 Laravel wrapper around Symfony Serializer.

2113.6k](/packages/wayofdev-laravel-symfony-serializer)[nilportugues/laravel5-json-api-dingo

Laravel5 JSONAPI and Dingo together to build APIs fast

311.5k](/packages/nilportugues-laravel5-json-api-dingo)[nicklaw5/larapi

A simple Laravel 5 class for handling json api responses.

111.5k](/packages/nicklaw5-larapi)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
