wfeller / parental
A simple eloquent trait that allows relationships to be accessed through child models.
Requires
- php: ^8.0
- gnugat/nomo-spaco: ~0.4
- illuminate/database: ^9.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0
- phpunit/phpunit: ^8.0|^9.0
- dev-master
- 0.13.0
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.0
- 0.10.1
- 0.10
- 0.9.1
- 0.9
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.1-alpha.4
- 0.6.1-alpha.3
- 0.6.1-alpha.2
- 0.6.1-alpha.1
- 0.6.1-alpha
- v0.6
- v0.5
- v0.4-alpha.2
- v0.4-alpha.1
- v0.4-alpha
- v0.3-alpha
- v0.2-alpha.1
- v0.2-alpha
- v0.1.0
- v0.1-alpha
- dev-fix-double-listener-registration
- dev-child-scope
- dev-laravel-5.4-compatible
This package is auto-updated.
Last update: 2024-11-11 02:11:20 UTC
README
Parental is a Laravel package, forked from calebporzio/parental, 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.
Licence
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
You can buy trees here offset.earth/treeware
Read more about Treeware at treeware.earth
Installation
composer require "wfeller/parental"
Each time you add or remove child classes, you'll want to do the following:
php artisan parental:discover-children
This artisan command will simplify the following:
- Laravel Nova resource inheritance (when children don't already have a dedicated Nova resource)
- Adding child global scopes when querying parent
Simple Usage
// The "parent" class User extends Model { // }
// The "child" class Admin extends User { use \WF\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 WF\Parental\HasChildren; protected $fillable = ['type']; }
// A "child" class Admin extends User { use \WF\Parental\HasParent; }
// Another "child" class Guest extends User { use \WF\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 \WF\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 \WF\Parental\HasChildren; protected $fillable = ['parental_type']; protected $childColumn = 'parental_type'; }