Download the PHP package saintsystems/eloquent-transformable without Composer
On this page you can find all versions of the php package saintsystems/eloquent-transformable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download saintsystems/eloquent-transformable
More information about saintsystems/eloquent-transformable
Files in saintsystems/eloquent-transformable
Package eloquent-transformable
Short Description Work with your Laravel Eloquent models the way you want them to look, not as they are.
License MIT
Informations about the package eloquent-transformable
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
You can install the package in to any Laravel 5.8.*
via composer:
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 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
-
Create a base
Model.php
class in your project and add theTransformable
trait to it. - Create a Model that represents your "Actual" database model.
Assuming a table definition of:
Table Name: tbl_Database_Table
Column | Type |
---|---|
PK_Database_ID | int |
DB_Name | varchar |
FK_Foreign_Key_ID | int |
-
Create a Model that represents your "Desired" database model.
- Use your new "Transformed" model the way you want to:
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
- Adam Anderly
- Saint Systems
- All Contributors
License
The MIT License (MIT). Please see License File for more information.