PHPackages                             dive-be/eloquent-super - 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. dive-be/eloquent-super

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

dive-be/eloquent-super
======================

Lightweight MTI (Multi-Table Inheritance) support for Eloquent models

1.3.0(2y ago)198.4k2[2 issues](https://github.com/dive-be/eloquent-super/issues)MITPHPPHP ~8.3

Since Feb 19Pushed 2y agoCompare

[ Source](https://github.com/dive-be/eloquent-super)[ Packagist](https://packagist.org/packages/dive-be/eloquent-super)[ Docs](https://github.com/dive-be/eloquent-super)[ RSS](/packages/dive-be-eloquent-super/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (7)Dependencies (6)Versions (8)Used By (0)

🦸🏼‍♂️ - Eloquent Super
======================

[](#‍️---eloquent-super)

Lightweight MTI (Multiple Table Inheritance) support for Eloquent models.

[![Latest Version on Packagist](https://camo.githubusercontent.com/2c1102087a37a712fc242c379f917f018f24acedb7d14d8063c86b4bb18a9e52/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646976652d62652f656c6f7175656e742d73757065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dive-be/eloquent-super)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/ceecb09a1fac50a4766eecf24ea4a27bbfd6741d2b0a0e3ec71327827449fb22/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646976652d62652f656c6f7175656e742d73757065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dive-be/eloquent-super)

What is "Multiple Table Inheritance" exactly?
---------------------------------------------

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

[](#cant-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](https://github.com/calebporzio/parental) 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?
-------------------------------------

[](#what-problem-does-this-package-solve)

### Short answer

[](#short-answer)

As a matter of fact, it solves absolutely **nothing**. "Why this package, then?" you may ask. Well, read on.

### Long answer

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

[](#installation)

```
composer require dive-be/eloquent-super
```

Usage
-----

[](#usage)

### Super / parent class 👱🏻‍♂️

[](#super--parent-class-‍️)

#### Migrations

[](#migrations)

The super model should define a morphs relationship that follows Laravel's naming conventions: the model's singular name in snake case + `able`.

```
Schema::create('addresses', static function (Blueprint $table) {
    $table->id();
    $table->foreignId('country_id')->constrained();
    $table->morphs('addressable'); // ==> mandatory

    // ... other columns
});
```

#### Class definition

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

```
class Address extends Model
{
    protected $fillable = ['city', 'country_id', 'street', 'postal_code'];

    public function country(): BelongsTo
    {
        return $this->belongsTo(Country::class);
    }
}
```

### Sub / child classes 👶🏼

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

[](#class-definition-1)

```
class ShippingAddress extends Model
{
    use \Dive\EloquentSuper\InheritsFromSuper;

    protected $fillable = ['email', 'phone', 'contact_person', 'is_expedited', 'courier'];

    protected function getSuperClass(): string
    {
        return Address::class;
    }
}
```

```
class InvoiceAddress extends Model
{
    use \Dive\EloquentSuper\InheritsFromSuper;

    protected $fillable = ['company_id', 'email', 'fax', 'phone', 'language'];

    protected function getSuperClass(): string
    {
        return Address::class;
    }
}
```

Capabilities 💪
--------------

[](#capabilities-)

### Partitioning of data when saving a sub model

[](#partitioning-of-data-when-saving-a-sub-model)

```
$address = ShippingAddress::create($request->validated());

$address->getAttributes(); // 'email', 'phone', 'contact_person', 'is_expedited', 'courier'
$address->super->getAttributes(); // 'city', 'country_id', 'street', 'postal_code'
```

### Attribute / relationship retrieval from super model

[](#attribute--relationship-retrieval-from-super-model)

```
$address->city; // Ghent
$address->super->city; // Ghent

$address->country; // App\Models\Country { #2981 }
$address->super->country; // App\Models\Country { #2981 }
```

### Deleting the super along with the sub model

[](#deleting-the-super-along-with-the-sub-model)

```
$address->delete(); // Database transaction in the background
```

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

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

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Muhammed Sari](https://github.com/mabdullahsari)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity73

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

Recently: every ~253 days

Total

7

Last Release

840d ago

Major Versions

0.3 → 1.02021-09-11

PHP version history (6 changes)0.1PHP ^7.4|^8.0

0.2PHP &gt;=8.0

1.0PHP ^8.0

1.1PHP ^8.1

1.2.0PHP ~8.2

1.3.0PHP ~8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/59749?v=4)[Artem Loenko](/maintainers/dive)[@dive](https://github.com/dive)

---

Top Contributors

[![mabdullahsari](https://avatars.githubusercontent.com/u/24608797?v=4)](https://github.com/mabdullahsari "mabdullahsari (20 commits)")

---

Tags

multitableinheritancediveeloquent-supermti

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/dive-be-eloquent-super/health.svg)

```
[![Health](https://phpackages.com/badges/dive-be-eloquent-super/health.svg)](https://phpackages.com/packages/dive-be-eloquent-super)
```

###  Alternatives

[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k43.2M626](/packages/spatie-laravel-medialibrary)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M94](/packages/mongodb-laravel-mongodb)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)

PHPackages © 2026

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