PHPackages                             saintsystems/eloquent-transformable - 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. [Database &amp; ORM](/categories/database)
4. /
5. saintsystems/eloquent-transformable

ActiveLibrary[Database &amp; ORM](/categories/database)

saintsystems/eloquent-transformable
===================================

Work with your Laravel Eloquent models the way you want them to look, not as they are.

0.0.5(7y ago)116MITPHPPHP &gt;=7.1.0

Since Mar 13Pushed 7y ago3 watchersCompare

[ Source](https://github.com/saintsystems/eloquent-transformable)[ Packagist](https://packagist.org/packages/saintsystems/eloquent-transformable)[ RSS](/packages/saintsystems-eloquent-transformable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (0)

Eloquent Transformable
======================

[](#eloquent-transformable)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d1ce65d94c785bc967e3f222916bec0be177d019e80fda6579783ad42d2bcf20/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7361696e7473797374656d732f656c6f7175656e742d7472616e73666f726d61626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/saintsystems/eloquent-transformable)[![Total Downloads](https://camo.githubusercontent.com/a8064d0f1197801d23b7b0c2dbb071ca7af3ef4b8eab33c526cbf31827f2a6d4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7361696e7473797374656d732f656c6f7175656e742d7472616e73666f726d61626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/saintsystems/eloquent-transformable)[![Build Status](https://camo.githubusercontent.com/b316f07ec68744f356b9d1e8ea124461a0278e5e47913d90293bf90a686635a6/68747470733a2f2f7472617669732d63692e6f72672f7361696e7473797374656d732f656c6f7175656e742d7472616e73666f726d61626c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/saintsystems/eloquent-transformable)

Work with your Laravel Eloquent models the way you want them to look (not as they are) using a simple transformation layer.

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

[](#installation)

You can install the package in to any Laravel `5.8.*` via composer:

```
composer require saintsystems/eloquent-transformable
```

Use Case
--------

[](#use-case)

Laravel Eloquent is built on conventions. These conventions make certain assumptions like your primary keys being named `id` or the way foreign key columns should be named. You can override these conventions, but that requires a lot of configuration. Additionally, tools like [Laravel Nova](https://nova.laravel.com) assume the default Eloquent conventions. Configuring Nova and Eloquent to use difference naming conventions then becomes a pain and your code becomes brittle because it is tied explicitly to your unconventional database naming standards.

Databases aren't always under our control. They may be managed by DBAs, could be third-party systems, or there may simply be a legacy database that doesn't adhere to Laravel's conventions that we don't want to change or can't change to make it conform to Laravel's conventions. This could be in the form of unconventional table names, column prefixes, column naming conventions, foreign key names, etc. We don't always control the database over which a Laravel app might sit.

Eloquent Transformable allows you to define how you would like the database columns to look with a simple transformation and then use your Eloquent Models as if they did adhere to Eloquent's naming conventions.

Transformable can also be used as a simple transformation layer to shield your application from the underlying database structure.

Usage
-----

[](#usage)

1. Create a base `Model.php` class in your project and add the `Transformable` trait to it.

```
    namespace App;

    use Illuminate\Database\Eloquent\Model as EloquentModel;
    use SaintSystems\Eloquent\Transformable\Transformable;

    class Model extends EloquentModel
    {
        use Transformable;
    }
```

2. Create a Model that represents your "Actual" database model.

Assuming a table definition of:

**Table Name:** `tbl_Database_Table`

ColumnTypePK\_Database\_IDintDB\_NamevarcharFK\_Foreign\_Key\_IDint```
    namespace App;

    class ActualDatabaseModel extends Model
    {
        protected $table = 'tbl_Database_Table';

        protected $primaryKey = 'PK_Database_ID';

        protected $guarded = [];
    }
```

3. Create a Model that represents your "Desired" database model.

```
namespace App;

class DesiredDatabaseModel extends ActualDatabaseModel
{
    // Desired $primaryKey name (PK_Database_ID is the actual PK in the database)
    protected $primaryKey = 'id';

    /**
     * Transformation Mapping of DB Column Names to desired Eloquent Model Attribute Names
     * This variable comes from the SaintSystems\Eloquent\Transformable\Transformable Trait
     * used in the base Model.php
     * @var array
     */
    protected $transform = [
        'id' => 'PK_Database_ID',
        'name' => 'DB_Name',
        'foreign_key_id' => 'FK_DB_Foreign_Key_ID'
    ]; // TransformationMap;
}
```

4. Use your new "Transformed" model the way you want to:

```
    $model = new DesiredDatabaseModel([
        'id' => 1,
        'name' => 'Name',
        'foreign_key_id' => 2
    ]);

    dd($desiredModel->toArray());
    /*
    Will output the following:
    [
        'id' => 1,
        'name' => 'Name',
        'foreign_key_id' => 2
    ]
    */

    // Now, save the model
    $model->save();
    // Despite using transformed attributes above, the record will still save using the transformed attributes we defined.

    // We can even query the model with our desired/transformed column names
    $model = new DesiredDatabaseModel::where('name','Joe')->orWhere('name','Judy')->get();

    /*
        The call above will result in the following query being run:
        select *
        from "tbl_Database_Table"
        where "DB_Name" = 'Joe' or "DB_Name" = 'Judy'

        But will come back in the following structure:
        [
            [
                'id' => 1,
                'name' => 'Joe',
                'foreign_key_id' => 1
            ],
            [
                'id' => 2,
                'name' => 'Judy',
                'foreign_key_id' => 1
            ]
        ]
    */
```

Benefits
--------

[](#benefits)

Using Eloquent Transformable we can build our app around Laravel Eloquent's conventions and use our models as if the underlying database had been built to Laravel's conventions. If we have time and are able to move our database structure to Laravel's conventions eventually, we can simply remove the transformation from our models. This shields us from underlying database changes and allows us to control the appearance of our how our underlying database is exposed in our apps or apis.

Credits
-------

[](#credits)

- [Adam Anderly](https://github.com/anderly)
- [Saint Systems](https://github.com/saintsystems)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 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 ~0 days

Total

5

Last Release

2613d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/65f595c1144918633b20511e1a6ae44a2756ac9f155527cbeb8072b4b219d664?d=identicon)[saintsystems](/maintainers/saintsystems)

---

Top Contributors

[![anderly](https://avatars.githubusercontent.com/u/573151?v=4)](https://github.com/anderly "anderly (12 commits)")

---

Tags

eloquenteloquent-modelslaraveltransformtransformationslaraveleloquenttransform

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/saintsystems-eloquent-transformable/health.svg)

```
[![Health](https://phpackages.com/badges/saintsystems-eloquent-transformable/health.svg)](https://phpackages.com/packages/saintsystems-eloquent-transformable)
```

###  Alternatives

[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

394388.0k5](/packages/rtconner-laravel-likeable)[shiftonelabs/laravel-cascade-deletes

Adds application level cascading deletes to Eloquent Models.

163632.1k2](/packages/shiftonelabs-laravel-cascade-deletes)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)[cybercog/laravel-nova-ban

A Laravel Nova banning functionality for your application.

40199.8k](/packages/cybercog-laravel-nova-ban)[phaza/single-table-inheritance

Single Table Inheritance Trait

1515.8k](/packages/phaza-single-table-inheritance)

PHPackages © 2026

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