PHPackages                             proai/eloquent-inheritance - 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. proai/eloquent-inheritance

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

proai/eloquent-inheritance
==========================

Single table inheritance for Laravel inspired by the Rails implementation

v0.1.7(3mo ago)21.1kMITPHPPHP ^8.0CI passing

Since Jun 4Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/ProAI/eloquent-inheritance)[ Packagist](https://packagist.org/packages/proai/eloquent-inheritance)[ Docs](http://github.com/ProAI/eloquent-inheritance)[ RSS](/packages/proai-eloquent-inheritance/feed)WikiDiscussions main Synced today

READMEChangelog (8)Dependencies (9)Versions (9)Used By (0)

Eloquent Inheritance
====================

[](#eloquent-inheritance)

[![Latest Stable Version](https://camo.githubusercontent.com/79d0a4153d68ef8bf42cc502f487b37dd970e643022f229c3bff3006f6494ae9/68747470733a2f2f706f7365722e707567782e6f72672f70726f61692f656c6f7175656e742d696e6865726974616e63652f762f737461626c65)](https://packagist.org/packages/proai/eloquent-inheritance) [![Total Downloads](https://camo.githubusercontent.com/4454e5260b15e8deec116122ed21d9821cd52d0b6e40ca4e0adaf3718fcabf1a/68747470733a2f2f706f7365722e707567782e6f72672f70726f61692f656c6f7175656e742d696e6865726974616e63652f646f776e6c6f616473)](https://packagist.org/packages/proai/eloquent-inheritance) [![Latest Unstable Version](https://camo.githubusercontent.com/597b79694b3f44382db040839641ba5a94d478e7320bfa5559b2241a57d509e5/68747470733a2f2f706f7365722e707567782e6f72672f70726f61692f656c6f7175656e742d696e6865726974616e63652f762f756e737461626c65)](https://packagist.org/packages/proai/eloquent-inheritance) [![License](https://camo.githubusercontent.com/9c52aacef6971639eb8eb6fe101885b656bbe9b3d1d7b26d247316a46735d337/68747470733a2f2f706f7365722e707567782e6f72672f70726f61692f656c6f7175656e742d696e6865726974616e63652f6c6963656e7365)](https://packagist.org/packages/proai/eloquent-inheritance)

Single table inheritance is a way to emulate object-oriented inheritance in a relational database. While other frameworks like Ruby on Rails have a built-in implementation for this pattern, Laravel has not. This package aims to provide an as easy as possible implementation for Laravel.

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

[](#installation)

You can install the package via composer:

```
composer require proai/eloquent-inheritance
```

Please note that you need at least **PHP 8.0** and **Laravel 9** for this package.

Usage
-----

[](#usage)

First you need to add the `Inheritance` trait to your root model:

```
use ProAI\Inheritance\Inheritance;

class Pet extends Model
{
    use Inheritance;

    //
}
```

Then you can extend the root model by other models that use the same table:

```
class Cat extends Pet
{
    //
}

class Dog extends Pet
{
    //
}
```

> Note that you need a table `pets` with a column called `type` in order to make the example above work.

Whenever a `Cat` or `Dog` model is instantiated, the attribute `type` will be set to the classname of the class (e.g. `App\Models\Cat`).

Other than that there is no magic and a `Cat` or a `Dog` model will behave just like a normal Eloquent model. You can define cat or dog specific attributes and relationships on these models. An attribute only set for dogs for example should be a nullable column on the table, so that it is set for dogs but `null` for other pets.

### Root model behaviour

[](#root-model-behaviour)

You can query the root model like any other model. However, the returned models will be transformed based by the type name. For example if there is one cat and one dog in the database, `Pet::all();` will return one `Dog` and one `Cat` model.

It is not possible to use `new Pet($attributes);` when you specify the type in `$attributes`. Please use the static method `Pet::new($attributes)` instead. This method returns a new model based on the given type. For example if the type in `$attributes` is `App\Models\Cat`, it will return an instance of `App\Models\Cat`. If no type is specified, an instance of `App\Models\Pet` will be returned.

### Custom type names

[](#custom-type-names)

If you want to use different names for the type column than the classname, you can use the static `$inheritanceMap` property:

```
class Pet extends Model
{
    use Inheritance;

    protected static $inheritanceMap = [
        'cat' => Cat::class,
        'dog' => Dog::class,
    ];
}
```

### Custom type column

[](#custom-type-column)

By default the name of the type column is `type`. However, you can set a custom type column name:

```
use ProAI\Inheritance\Inheritance;

class Pet extends Model
{
    use Inheritance;

    protected static $inheritanceColumn = 'pet_type';
}
```

Support
-------

[](#support)

Bugs and feature requests are tracked on [GitHub](https://github.com/proai/eloquent-inheritance/issues).

License
-------

[](#license)

This package is released under the [MIT License](LICENSE).

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance82

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

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

Recently: every ~232 days

Total

8

Last Release

91d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/54da07fafc9e45d312ea3094f27da887d232ec5de9b28b5a3073eb76b7874214?d=identicon)[markusjwetzel](/maintainers/markusjwetzel)

---

Top Contributors

[![markusjwetzel](https://avatars.githubusercontent.com/u/6650637?v=4)](https://github.com/markusjwetzel "markusjwetzel (19 commits)")

---

Tags

laravelormeloquenttablesingleinheritance

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/proai-eloquent-inheritance/health.svg)

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

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[nanigans/single-table-inheritance

Single Table Inheritance Trait

2512.6M3](/packages/nanigans-single-table-inheritance)[phaza/single-table-inheritance

Single Table Inheritance Trait

1515.8k](/packages/phaza-single-table-inheritance)[lemaur/eloquent-publishing

218.1k1](/packages/lemaur-eloquent-publishing)

PHPackages © 2026

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