Download the PHP package dive-be/eloquent-super without Composer
On this page you can find all versions of the php package dive-be/eloquent-super. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dive-be/eloquent-super
More information about dive-be/eloquent-super
Files in dive-be/eloquent-super
Package eloquent-super
Short Description Lightweight MTI (Multi-Table Inheritance) support for Eloquent models
License MIT
Homepage https://github.com/dive-be/eloquent-super
Informations about the package eloquent-super
π¦ΈπΌββοΈ - Eloquent Super
Lightweight MTI (Multiple Table Inheritance) support for Eloquent models.
What is "Multiple Table Inheritance" exactly?
MTI allows one to have separate database tables for each "sub class" that shares a common "super class".
"Can't I just define a type column in my table and call it a day?"
Well, it depends. If the only thing you'd like to do is adding specific behavior to each sub type (user
- admin
for example), then Single Table Inheritance is definitely the better choice here.
However, if the sub types have very different data fields, then MTI is the better tool. Using STI in this case will cause the table in question to have a lot of NULL
columns.
What problem does this package solve?
Short answer
As a matter of fact, it solves absolutely nothing. "Why this package, then?" you may ask. Well, read on.
Long answer
You see, Eloquent already gives us the ability to define polymorphic relationships. The only thing you need to start leveraging MTI capabilities in Eloquent is a MorphOne
relationship. This package adds a nice DX layer on top of the existing functionality, so it is a tad nicer to work with these kind of tightly coupled relationships.
So, the "meat and potatoes" of this package is delegating calls to the defined super
relationship (and a couple more things). There is no real "parent" class in an object oriented sense. It is a conscious decision to not sprinkle too much magic on the models.
Installation
Usage
Super / parent class π±π»ββοΈ
Migrations
The super model should define a morphs relationship that follows Laravel's naming conventions: the model's singular name in snake case + able
.
Class definition
The super class must define a fillable array in order to determine which attributes belong to which database tables. Without it, there is no way to distinguish the super's columns from the sub's columns.
Sub / child classes πΆπΌ
It is not mandatory for the sub classes to define a fillable array. Setting $guarded
to an empty array is perfectly fine as well.
Class definition
Capabilities πͺ
Partitioning of data when saving a sub model
Attribute / relationship retrieval from super model
Deleting the super along with the sub model
Note: only the sub model will be trashed if both the super and sub use the "SoftDeletes" trait
A note on always eager loading the "super" relationship π£
It does not make sense for the sub model to exist without its complementary data from the super model. By having two tables, we are able to achieve a normalized database, but in code, it only makes sense when they coexist as a whole.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Muhammed Sari
- All Contributors
License
The MIT License (MIT). Please see License File for more information.