Eloquent Single Table Inheritance
![StyleCI][ico-style-ci]
![Total Downloads][ico-downloads]
![Software License][ico-license], (*1)
Single Table Inheritance for Laravel's Eloquent ORM., (*2)
Install
Via Composer, (*3)
``` bash
$ composer require jonnypickett/eloquent-sti, (*4)
After updating composer, add the ServiceProvider to the providers array in config/app.php
``` php
JonnyPickett\EloquentSTI\ServiceProvider::class,
Usage
Use the SingleTableInheritance trait in any Eloquent model to take advantage of Single Table Inheritance in Laravel with the model's subclasses., (*5)
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., (*6)
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, (*7)
``` php
class Animal extends Model
{
use SingleTableInheritance;, (*8)
protected $table = 'animals';
protected $fillable = [
'subclass_name',
...
];
protected $noise = '';
/**
* @return string
*/
public function speak()
{
return $this->noise;
}
}, (*9)
Now just extend the parent model with any child models
```php
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., (*10)
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, (*11)
ELOQUENT_STI_SUBCLASS_FIELD=your_subclass_field_name_here
or publishing and updating the package configuration file in app/config/eloquent-sti.php, (*12)
php artisan vendor:publish --provider="JonnyPickett\EloquentSTI\ServiceProvider"
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Subclass Field
|--------------------------------------------------------------------------
|
| Subclass class names will be stored and retrieved from this field.
| This can be overridden in specific classes by setting the
| protected property $subclassFiled to a different value
| in the class definition.
|
*/
'subclass_field' => env('ELOQUENT_STI_SUBCLASS_FIELD', 'your_subclass_field_name_here'),
];
or can be changed on a per model basis by adding a $subclassField property to your parent model, (*13)
class Animal extends Model
{
protected $subclassField = 'your_subclass_field_name_here'
}
Credit
Inspiration for this package came way back when I came across this Laravel.io forum post by Shawn McCool, (*14)
License
The MIT License (MIT). Please see License File for more information., (*15)