PHP code example of cvsouth / eloquent-inheritance

1. Go to this page and download the library: Download cvsouth/eloquent-inheritance 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/ */

    

cvsouth / eloquent-inheritance example snippets


composer san migrate

class Animal extends InheritableModel
{
    public $table = "animals";
    
    protected $fillable =
    [
        'name',
        'species',
    ];
    
    public function speak()
    {
        print($this->name . ' makes a noise');
    }
}

class Bird extends Animal
{
    public $table = "birds";
    
    protected $fillable =
    [
        'flying',
    ];

    public function speak()
    {
        print('AAA!');
    }

    public function fly()
    {
        $this->flying = true;
    }

    public function land()
    {
        $this->flying = false;
    }

    public function isFlying()
    {
        return $this->flying;
    }
}

class CreateAnimalsTable extends Migration
{
    public function up()
    {
        Schema::create('animals', function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->bigInteger('base_id')->unsigned();
            $table->string('species', 250);
            $table->string('name', 250)->nullable();
        });
    }

    public function down()
    {
        Schema::drop('animals');
    }
}

class CreateBirdsTable extends Migration
{
    public function up()
    {
        Schema::create('birds', function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->bigInteger('base_id')->unsigned();
            $table->boolean('flying')->default(false);
        });
    }

    public function down()
    {
        Schema::drop('birds');
    }
}

$bird = new Bird
([
    "species" => "Aratinga solstitialis", // Note: This attribute is inherited from Animal
]);

$bird->fly();
echo "This " . strtolower($bird->species) . " is " . ($bird->isFlying() ? "" : "not ") . "flying" . '<br/>';
// This aratinga solstitialis is flying

$bird->species = 'Sun Conure';

$bird->save();

$bird->land();
echo "This " . strtolower($bird->species) . " is " . ($bird->isFlying() ? "" : "not ") . "flying" . '<br/>';
// This sun conure is not flying

$bird = Bird::where("species", "=", "Aratinga solstitialis")->first();

$bird->speak();
// AAA!

// The model's animal id
echo $bird->id_as(Animal::class);

// The model's bird id
echo $bird->id_as(Bird::class);

// The model's base id
echo $bird->base_id

class Trainer extends InheritableModel
{
    public $table = "trainers";
    protected $fillable =
    [
        'name',
        'animal_id',
    ];
    
    public function animal()
    {
        return $this->belongsTo(Animal::class);
    }
}

class CreateTrainersTable extends Migration
{
    public function up()
    {
        Schema::create('trainers', function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->bigInteger('base_id')->unsigned();
            $table->string('name', 250)->nullable();
            $table->bigInteger('animal_id')->unsigned();
        });
        
        Schema::table('trainers', function ($table)
        {
            $table->foreign('animal_id')->references('id')->on('animals')->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::drop('trainers');
    }
}

$bird = Bird::where("species", "=", "Aratinga solstitialis")->first();
$trainer = new Trainer
([
    "name" => "Trainer 1",
    "animal_id" => $bird->id_as(Animal::class), // Reference the bird's Animal ID
]);
$trainer->save();

echo class_basename($trainer->animal); // Bird
echo $trainer->animal->species; // Sun Conure