parental

Use single table inheritance in your Laravel app

  • 所有者: tighten/parental
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Parental - Use single table inheritance in your Laravel App

Parental

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

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.

Installation

composer require "calebporzio/parental=0.9"

Simple Usage

// The "parent"
class User extends Model
{
    //
}
// The "child"
class Admin extends User
{
    use \Parental\HasParent;

    public function impersonate($user) {
        ...
    }
}
// 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.

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();
});
// The "parent"
class User extends Model
{
    use \Parental\HasChildren;

    protected $fillable = ['type'];
}
// A "child"
class Admin extends User
{
    use \Parental\HasParent;
}
// Another "child"
class Guest extends User
{
    use \Parental\HasParent;
}
// 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).

Type Aliases

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

class User extends Model
{
    use \Parental\HasChildren;

    protected $fillable = ['type'];

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

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

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

Custom Type Column Name

You can override the default type column by setting the $childColumn property on the parent model.

class User extends Model
{
    use \Parental\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:

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.

主要指標

概覽
名稱與所有者tighten/parental
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2017-08-01 15:53:38
推送於2025-04-15 17:06:34
最后一次提交
發布數32
最新版本名稱v1.4.3 (發布於 2025-04-15 14:06:24)
第一版名稱v0.1.0 (發布於 )
用户参与
星數1.4k
關注者數35
派生數103
提交數199
已啟用問題?
問題數60
打開的問題數10
拉請求數53
打開的拉請求數5
關閉的拉請求數25
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?