Skip to main content
Back to packages
1,530 GitHub stars

tighten/parental

View on GitHub

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

Roles and Permissions in Laravel 13

14 lessons
57 min

Queues in Laravel 13

18 lessons
1 h 12 min read

How to Build Laravel 13 API From Scratch

30 lessons
1 h 23 min

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.