PHPackages                             srlabs/eloquent-sti - 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. srlabs/eloquent-sti

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

srlabs/eloquent-sti
===================

Implement Single Table Inheritance within the Eloquent ORM

v6.0.0(4y ago)93.8k3[1 issues](https://github.com/stagerightlabs/eloquent-sti/issues)MITPHPPHP ^8.0

Since Jan 19Pushed 4y ago2 watchersCompare

[ Source](https://github.com/stagerightlabs/eloquent-sti)[ Packagist](https://packagist.org/packages/srlabs/eloquent-sti)[ RSS](/packages/srlabs-eloquent-sti/feed)WikiDiscussions dev Synced 1mo ago

READMEChangelogDependencies (2)Versions (13)Used By (0)

Eloquent Single Table Inheritance
=================================

[](#eloquent-single-table-inheritance)

This package provides an easy way to extend Eloquent model objects to provide support for single table inheritance. Inspired by an [article](http://snooptank.com/single-table-inheritance-with-eloquent-laravel-4/) written by Pallav Kaushish.

### Installation

[](#installation)

This package should be installed via composer:

```
$ composer require srlabs/eloquent-sti
```

### Usage

[](#usage)

In your Model, add the `SingleTableInheritanceTrait` trait and then specify these configuration values:

- `table`: The name of the database table assigned to the base model
- `morphClass`: The full class name of the base Eloquent Object
- `discriminatorColumn`: The column in the database table used to distinguish STI entity types
- `inheritanceMap`: An array mapping discriminator column values to corresponding entity classes

Here is an imaginary Widget model as an example:

```
// app/Epiphyte/Widget.php
class Widget extends Illuminate\Database\Eloquent\Model {

    /*****************************************************************************
     *  Eloquent Configuration
     *****************************************************************************/

    protected $guarded = ['id'];
    protected $fillable = ['name', 'description', 'status'];

    /*****************************************************************************
     * Single Table Inheritance Configuration
     *****************************************************************************/

    use SingleTableInheritanceTrait;
    protected $table = 'widgets';
    protected $morphClass = 'Epiphyte\Widget';
    protected $discriminatorColumn = 'status';
    protected $inheritanceMap = [
        'new' => 'Epiphyte\Entities\Widgets\NewWidget',
        'processed' => 'Epiphyte\Entities\Widgets\ProcessedWidget'
        'complete' => 'Epiphyte\Entities\Widgets\CompleteWidget'
    ];

    // ...
}
```

Next you need to create each of your child entity classes. I often keep them in an `Entities` folder, but any namespaced location will work.

Here is a hypothetical example:

```
// app/Epiphyte/Entities/Widgets/NewWidget.php
class NewWidget extends Epiphyte\Widget {

    /**
     * Limit the query scope if we define a query against the base table using this class.
     *
     * @param bool $excludeDeleted
     *
     * @return $this
     */
    public function newQuery($excludeDeleted = true)
    {
        return parent::newQuery($excludeDeleted)->where('status', '=', 'new');
    }

    // Remaining child entity methods go here...
}
```

Whenever Eloquent wants to return an instance of the base model it will use the value of the discriminator column to determine the appropriate entity type and return an instance of that class instead. This holds true for all Eloquent actions but it will not work on direct database (i.e. `DB::table()`) calls.

Providing the `newQuery()` method in the child class will allow you to use the Entity as a traditional Eloquent accessor that only returns entities of its own type. In this case, `NewWidget::all();` would return all of the widgets flagged as 'new' in the database.

Any questions about this package should be posted on the [package website](http://stagerightlabs.com/projects/eloquent-sti).

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 96% 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 ~257 days

Recently: every ~593 days

Total

11

Last Release

1551d ago

Major Versions

v1.0.1 → v2.0.02015-03-25

v2.0.3 → v3.0.02019-09-04

v3.0.0 → v4.0.02020-03-04

v4.0.0 → v5.0.02020-09-09

v5.0.0 → v6.0.02022-02-09

PHP version history (4 changes)v1.0.0PHP &gt;=5.4.0

v3.0.0PHP ^7.2

v5.0.0PHP ^7.3

v6.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/97d8f58fda6a474519e876ece356e8246390e4e7f3b2c56ee8ec6bc1e88cfa6b?d=identicon)[Stage Right Labs](/maintainers/Stage%20Right%20Labs)

---

Top Contributors

[![rydurham](https://avatars.githubusercontent.com/u/674347?v=4)](https://github.com/rydurham "rydurham (24 commits)")[![gausie](https://avatars.githubusercontent.com/u/179758?v=4)](https://github.com/gausie "gausie (1 commits)")

---

Tags

laraveleloquenttraitsingle-table-inheritance

### Embed Badge

![Health badge](/badges/srlabs-eloquent-sti/health.svg)

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

###  Alternatives

[cybercog/laravel-ban

Laravel Ban simplify blocking and banning Eloquent models.

1.1k651.8k11](/packages/cybercog-laravel-ban)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[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)

PHPackages © 2026

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