2017 © Pedro Peláez
 

library parental

A simple eloquent trait that allows relationships to be accessed through child models.

image

tightenco/parental

A simple eloquent trait that allows relationships to be accessed through child models.

  • Monday, July 30, 2018
  • by calebporzio
  • Repository
  • 18 Watchers
  • 153 Stars
  • 337 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 3 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

Parental - Use single table inheritance in your Laravel App, (*1)

Parental

Parental is a Laravel package that brings STI (Single Table Inheritance) capabilities to Eloquent., (*2)

What is single table inheritance (STI)?

It's a fancy name for a simple concept: Extending a model (usually to add specific behavior), but referencing the same table., (*3)

Installation

composer require tightenco/parental

Simple Usage

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Parental\HasChildren;

// The "parent"
class User extends Model
{
    use HasChildren;
    //
}
namespace App\Models;

use Parental\HasParent;

// The "child"
class Admin extends User
{
    use HasParent;

    public function impersonate($user) {
        //...
    }
}
use App\Models\Admin;

// Returns "Admin" model, but reference "users" table:
$admin = Admin::first();

// Can now access behavior exclusive to "Admin"s
$admin->impersonate($user);

What problem did we just solve?

Without Parental, calling Admin::first() would throw an error because Laravel would be looking for an admins table. Laravel generates expected table names, as well as foreign keys and pivot table names, using the model's class name. By adding the HasParent trait to the Admin model, Laravel will now reference the parent model's class name users., (*4)

Accessing Child Models from Parents

// First, we need to create a `type` column on the `users` table
Schema::table('users', function ($table) {
    $table->string('type')->nullable();
});
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Parental\HasChildren;

// The "parent"
class User extends Model
{
    use HasChildren;

    protected $fillable = ['type'];
}
namespace App\Models;

use Parental\HasParent;

// A "child"
class Admin extends User
{
    use HasParent;
}
namespace App\Models;

use Parental\HasParent;

// Another "child"
class Guest extends User
{
    use HasParent;
}
use App\Models\Admin;
use App\Models\Guest;
use App\Models\User;

// Adds row to "users" table with "type" column set to: "App/Admin"
Admin::create(...);

// Adds row to "users" table with "type" column set to: "App/Guest"
Guest::create(...);

// Returns 2 model instances: Admin, and Guest
User::all();

What problem did we just solve?

Before, if we ran: User::first() we would only get back User models. By adding the HasChildren trait and a type column to the users table, running User::first() will return an instance of the child model (Admin or Guest in this case)., (*5)

Type Aliases

If you don't want to store raw class names in the type column, you can override them using the $childTypes property., (*6)

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Parental\HasChildren;

class User extends Model
{
    use HasChildren;

    protected $fillable = ['type'];

    protected $childTypes = [
        'admin' => Admin::class,
        'guest' => Guest::class,
    ];
}

Now, running Admin::create() will set the type column in the users table to admin instead of App\Models\Admin., (*7)

This feature is useful if you are working with an existing type column, or if you want to decouple application details from your database., (*8)

Custom Type Column Name

You can override the default type column by setting the $childColumn property on the parent model., (*9)

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Parental\HasChildren;

class User extends Model
{
    use HasChildren;

    protected $fillable = ['parental_type'];

    protected $childColumn = 'parental_type';
}

Laravel Nova Support

If you want to use share parent Nova resources with child models, you may register the following provider at the end of the boot method of your NovaServiceProvider:, (*10)

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    public function boot() {
        parent::boot();
        // ...
        $this->app->register(\Parental\Providers\NovaResourceProvider::class);
    }
}

Thanks to @sschoger for the sick logo design, and @DanielCoulbourne for helping brainstorm the idea on Twenty Percent Time., (*11)

The Versions

30/07 2018

dev-master

9999999-dev

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

30/07 2018

v0.4-alpha.2

0.4.0.0-alpha2

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

29/06 2018

v0.4-alpha.1

0.4.0.0-alpha1

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

28/06 2018

v0.4-alpha

0.4.0.0-alpha

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

22/06 2018

v0.3-alpha

0.3.0.0-alpha

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

21/06 2018

v0.2-alpha.1

0.2.0.0-alpha1

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

08/06 2018

v0.2-alpha

0.2.0.0-alpha

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

06/09 2017

v0.1-alpha

0.1.0.0-alpha

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

01/09 2017

dev-laravel-5.4-compatible

dev-laravel-5.4-compatible

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio

01/09 2017

v0.1.0

0.1.0.0

A simple eloquent trait that allows relationships to be accessed through child models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Porzio