Description
Brings STI (Single Table Inheritance) capabilities to Eloquent.
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 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.
Recent Courses on Laravel Daily
Laravel 13 Eloquent: Expert Level
41 lessons
1 h 34 min
Queues in Laravel 13
18 lessons
1 h 12 min read
[FREE] Laravel 13 for Beginners: 3 Demo Projects
5 lessons
29 min