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

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

jonnypickett/eloquent-sti
=========================

Single Table Inheritance for Eloquent ORM

1.1.0(9y ago)51.2k↓50%3[1 issues](https://github.com/jonnypickett/eloquent-sti/issues)MITPHPPHP ~5.6|~7.0

Since Jan 9Pushed 9y ago1 watchersCompare

[ Source](https://github.com/jonnypickett/eloquent-sti)[ Packagist](https://packagist.org/packages/jonnypickett/eloquent-sti)[ Docs](https://github.com/jonnypickett/eloquent-sti)[ RSS](/packages/jonnypickett-eloquent-sti/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (3)Used By (0)

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

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

[![Built For Laravel](https://camo.githubusercontent.com/5dc33dc453936987e8423b2e93ebdb1955ecc7187cc664824ca4a957aecb85c2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c74253230666f722d6c61726176656c2d626c75652e737667)](http://laravel.com)[![Build Status](https://camo.githubusercontent.com/332f441387951fe17a66edd4fb8e6a36512d454effd58ddda49a72402d1e5ff4/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a6f6e6e797069636b6574742f656c6f7175656e742d7374692f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/jonnypickett/eloquent-sti)[![StyleCI](https://camo.githubusercontent.com/730408b22817f7d3064eca83392dce7cf5c87882d39bfae48bd27294b7665c97/68747470733a2f2f7374796c6563692e696f2f7265706f732f34353439373035352f736869656c643f7374796c653d666c6174)](https://styleci.io/repos/78478485)[![Latest Version on Packagist](https://camo.githubusercontent.com/adc12c506d8a21e75cbdf10f1fa805804137831b53a338eb4b444b551fdee132/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f6e6e797069636b6574742f656c6f7175656e742d7374692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jonnypickett/eloquent-sti)[![Total Downloads](https://camo.githubusercontent.com/7d5734038e6ca0f02a64371fd3da79a8a71bd81525e006bd860199850ffd2bc0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6f6e6e797069636b6574742f656c6f7175656e742d7374692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jonnypickett/eloquent-sti)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Single Table Inheritance for Laravel's Eloquent ORM.

Install
-------

[](#install)

Via Composer

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

After updating composer, add the ServiceProvider to the providers array in config/app.php

```
JonnyPickett\EloquentSTI\ServiceProvider::class,
```

Usage
-----

[](#usage)

Use the SingleTableInheritance trait in any Eloquent model to take advantage of Single Table Inheritance in Laravel with the model's subclasses.

A table taking advantage of Single Table Inheritance needs to store subclass names. By default, this package uses a field named `subclass_name`, so add this field, or the field name you choose (configuration is shown later), to your table.

```
Schema::table('animals', function(Blueprint $table)
{
    $table->string('subclass_name');
}
```

Once this field is added to your database table, you will need to add the `SingleTableInheritance` trait, make sure to specify the table property, and add the subclass name field to the `$fillable` array all on the parent model definition

```
class Animal extends Model
{
    use SingleTableInheritance;

    protected $table = 'animals';

    protected $fillable = [
        'subclass_name',
        ...
    ];

    protected $noise = '';

    /**
     * @return string
     */
    public function speak()
    {
        return $this->noise;
    }
}
```

Now just extend the parent model with any child models

```
class Dog extends Animal
{
    protected $noise = 'ruff';
}

class Cat extends Animal
{
    protected $noise = 'meow';
}
```

That's it. Now the entries in your table, `animals` for our example, will always be returned as an instance of a specific subclass. When retrieving a collection, the collection will be a collection of various subclass instances.

Configuration
-------------

[](#configuration)

By default, the subclass names will be stored in a field named `subclass_name`. This can be changed project wide by updating your .env file

```
ELOQUENT_STI_SUBCLASS_FIELD=your_subclass_field_name_here
```

or publishing and updating the package configuration file in app/config/eloquent-sti.php

```
php artisan vendor:publish --provider="JonnyPickett\EloquentSTI\ServiceProvider"
```

```
