PHP code example of jonnypickett / eloquent-sti

1. Go to this page and download the library: Download jonnypickett/eloquent-sti library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jonnypickett / eloquent-sti example snippets


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

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

ELOQUENT_STI_SUBCLASS_FIELD=your_subclass_field_name_here



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'),
];


class Animal extends Model
{
    protected $subclassField = 'your_subclass_field_name_here'
}
 php
JonnyPickett\EloquentSTI\ServiceProvider::class,
 php
class Animal extends Model
{
    use SingleTableInheritance;
    
    protected $table = 'animals';
    
    protected $fillable = [
        'subclass_name',
        ...
    ];
    
    protected $noise = '';
    
    /**
     * @return string
     */
    public function speak()
    {
        return $this->noise;
    }
}
bash
php artisan vendor:publish --provider="JonnyPickett\EloquentSTI\ServiceProvider"