The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Single Table Inheritance for Eloquent Models Using Parental

Last updated on by

Single Table Inheritance for Eloquent Models Using Parental image

Parental is a Laravel package by Tighten that brings Single Table Inheritance (STI) to Eloquent. Instead of creating separate database tables for each model variation, Parental lets you extend a parent model with child classes that all share the same table, with a type column distinguishing between them.

Use Cases

Single Table Inheritance is useful when you have models that share most of the same attributes but differ in behavior. Common examples include:

  • User types - An Admin and a Guest that extend a base User model, each with their own methods and relationships
  • Content types - A TextPost and ImagePost that extend a Post model, with type-specific relationships like mentions or attachments
  • Order states - A PendingOrder and ShippedOrder that extend an Order, where state transitions change the model type

Getting Started

Install the package via Composer:

composer require tightenco/parental

Next, define your parent and child models by adding the HasChildren trait to the parent model and the HasParent trait to each child model. Each child class extends the parent and reads from and writes to the same database table:

use Illuminate\Database\Eloquent\Model;
use Tighten\Parental\HasChildren;
 
class Order extends Model
{
use HasChildren;
}
use Tighten\Parental\HasParent;
 
class PendingOrder extends Order
{
use HasParent;
}
use Tighten\Parental\HasParent;
 
class ShippedOrder extends Order
{
use HasParent;
}

When you create a PendingOrder or ShippedOrder, the record is stored in the orders table with a type column set to identify the child class.

Custom Type Column

If your table uses a column name other than type, override it with the $childColumn property on the parent model:

class Order extends Model
{
use HasChildren;
 
protected $childColumn = 'status';
}

Custom Type Aliases

By default, Parental stores the fully-qualified class name in the type column. You can map shorter aliases using the $childTypes property on the parent model:

use Illuminate\Database\Eloquent\Model;
use Tighten\Parental\HasChildren;
 
class Order extends Model
{
use HasChildren;
 
protected $childTypes = [
'pending' => PendingOrder::class,
'shipped' => ShippedOrder::class,
];
}

This stores pending or shipped in the database instead of the full class name. The type column also supports integer values if you prefer numeric identifiers:

protected $childTypes = [
1 => PendingOrder::class,
2 => ShippedOrder::class,
];

Transitioning Between Types with become()

The become() method lets you switch a model from one child type to another at runtime. For example, when an order moves through its lifecycle, you can re-cast it to a different class:

$order = PendingOrder::findOrFail(1);
 
$order = $order->become(ShippedOrder::class);
$order->save();

The call returns a fresh instance of the target class with all the original attributes carried over. Parental also dispatches a becoming event before the switch, so you can listen for type changes and run additional logic when they happen.

Eager Loading Child-Specific Relationships

When a query returns a mix of child types, you may need to load relationships that only exist on certain classes. Calling a standard load() with a relationship defined on just one child type would throw an error. Parental ships with dedicated helpers that handle this by scoping the eager load to the correct child class:

$orders->loadChildren([
PendingOrder::class => ['items'],
ShippedOrder::class => ['shipments'],
]);

These helpers work on Eloquent query builders, collections, and paginators:

// On queries
Order::childrenWith([
PendingOrder::class => ['items'],
ShippedOrder::class => ['shipments'],
])->get();
 
// Count child-specific relationships
Order::childrenWithCount([
PendingOrder::class => ['items'],
ShippedOrder::class => ['shipments'],
])->get();

To learn more about Parental and view the source code, visit the GitHub repository.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Jump24 - UK Laravel Agency

Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Visit Jump24 - UK Laravel Agency
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
The Inertia v3 Beta is Here image

The Inertia v3 Beta is Here

Read article
Polyscope Is an Ai-First Dev Environment for Orchestrating Agents image

Polyscope Is an Ai-First Dev Environment for Orchestrating Agents

Read article
Filament v5.3.0 Released with Deferred Tab Badges and Column Manager Improvements image

Filament v5.3.0 Released with Deferred Tab Badges and Column Manager Improvements

Read article
Ward: A Security Scanner for Laravel image

Ward: A Security Scanner for Laravel

Read article
Kit: An Opinionated API Starter Kit for Laravel image

Kit: An Opinionated API Starter Kit for Laravel

Read article
Livewire v4.2.0 Released with Security Hardening and Laravel 13 Support image

Livewire v4.2.0 Released with Security Hardening and Laravel 13 Support

Read article