2017 © Pedro Peláez
 

library eloquent-inheritance

Use a One-To-One (or One-To-Many) relation between a parent & child Tables to simulate inheritance between Models

image

igaster/eloquent-inheritance

Use a One-To-One (or One-To-Many) relation between a parent & child Tables to simulate inheritance between Models

  • Sunday, January 24, 2016
  • by igaster
  • Repository
  • 1 Watchers
  • 0 Stars
  • 70 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 25 % Grown

The README.md

Laravel License Downloads Build Status Codecov, (*1)

Description

Eloquent Multiple Table Inheritance: Use a One-To-One (or One-To-Many) relation between a parent & child Tables to simulate inheritance between Models., (*2)

Installation:

Edit your project's composer.json file to require:, (*3)

"require": {
    "igaster/eloquent-inheritance": "~1.0"
}

and install with composer update, (*4)

Usage:

Example Schema:

// Model Foo is the parent model
Schema::create('foo', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('a');
});

// Model Bar inherits Foo.
Schema::create('bar', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('b');
    $table->integer('foo_id')->nullable(); // Foreign Key to Foo
});

Example Models:

Your models should use the EloquentInherited Trait, (*5)

class Foo extends Eloquent
{
    use \igaster\EloquentInheritance\EloquentInherited;

    // ...
    public function fooMethod(){}
}

class Bar extends Eloquent
{
    use \igaster\EloquentInheritance\EloquentInherited;

    // ...
    public function barMethod(){}
}

class BarExtendsFoo extends igaster\EloquentInheritance\InheritsEloquent{
    // Set Parent/Child classes
    public static $parentClass = Foo::class;
    public static $childClass  = Bar::class;

    // You must declare Parent/Child keys explicity:
    public static $parentKeys = ['id','a'];
    public static $childKeys  = ['id','b','foo_id'];

    // Childs Foreign Key (points to Parent)
    public static $childFK  = 'foo_id';

    // You can add your functions / variables ...
    public function newMethod(){}
}

Usage:

// Create a composite Model:
$fooBar = BarExtendsFoo::create([ // Creates and Saves Foo & Bar in the Database
    'a' => 1,
    'b' => 2,
]);

// Access Attributes:
$fooBar->a; // = 1 (from Foo model)
$fooBar->b; // = 2 (from Bar model)

// Call Methods:
$fooBar->fooMethod(); // from Foo Model
$fooBar->barMethod(); // from Bar Model
$fooBar->newMethod(); // from self

// Query as an Eloquent model:
$fooBar = BarExtendsFoo::find(1);
$fooBar = BarExtendsFoo::where('a',1)->first();
$fooBar = BarExtendsFoo::get();     // Collection of BarExtendsFoo 

$fooBar->save();    // Saves Foo & Bar
$fooBar->delete();  // Deletes Foo & Bar
$fooBar->update([   // Updates Foo & Bar
    'a' => 10,
    'b' => 20,
]);

The Versions

24/01 2016

dev-master

9999999-dev https://github.com/igaster/eloquent-inheritance.git

Use a One-To-One (or One-To-Many) relation between a parent & child Tables to simulate inheritance between Models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giannis Gasteratos

laravel-5.x table inheritance eloquent inheritance multiple table inheritance

17/01 2016

v1.0.1

1.0.1.0 https://github.com/igaster/eloquent-inheritance.git

Use a One-To-One (or One-To-Many) relation between a parent & child Tables to simulate inheritance between Models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giannis Gasteratos

laravel-5.x table inheritance eloquent inheritance multiple table inheritance

05/01 2016

v1.0.0

1.0.0.0 https://github.com/igaster/eloquent-inheritance.git

Multiple table inheritance in Laravel Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giannis Gasteratos

laravel-5.x table inheritance eloquent inheritance