Try Depot: Bring ultra-fast, remote Docker builds directly to your Laravel workflow

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
Laravel Cloud

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

Visit Laravel Cloud
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
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 →
Laravel Live Denmark Returns to Copenhagen in August 2026 image

Laravel Live Denmark Returns to Copenhagen in August 2026

Read article
Generate Secure, Memorable Passphrases in PHP with PHP Passphrase image

Generate Secure, Memorable Passphrases in PHP with PHP Passphrase

Read article
FrankenPHP v1.11.2 Released With 30% Faster CGO, 40% Faster GC, and Security Patches image

FrankenPHP v1.11.2 Released With 30% Faster CGO, 40% Faster GC, and Security Patches

Read article
Capture Web Page Screenshots in Laravel with Spatie's Laravel Screenshot image

Capture Web Page Screenshots in Laravel with Spatie's Laravel Screenshot

Read article
Nimbus: An In-Browser API Testing Playground for Laravel image

Nimbus: An In-Browser API Testing Playground for Laravel

Read article
Laravel 12.51.0 Adds afterSending Callbacks, Validator whenFails, and MySQL Timeout image

Laravel 12.51.0 Adds afterSending Callbacks, Validator whenFails, and MySQL Timeout

Read article