findyouractivity/single-table-inheritance
Single-table inheritance for Laravel Eloquent: store multiple model subclasses in one database table and have queries return the correct child class. Configure type columns and mappings to keep a shared schema with distinct PHP models.
composer require <vendor>/<package> (or upgrade to ^1.1). If you are on PHP 8.0 or lower, stay on the previous minor until you upgrade PHP.enum types, so the quickest win is replacing string constants or custom enum classes with backed enums.enum Status: string {
case Draft = 'draft';
case Published = 'published';
}
class Post extends Model {
protected $casts = ['status' => Status::class];
}
// Form request
'status' => ['required', Rule::enum(Status::class)],
string or int) directly in $casts. The package resolves the enum instance on attribute access and stores the backing value on save.Rule::enum(Status::class) where available; the package may provide its own rule or polyfill for older Laravel versions. Use tryFrom() for untrusted input and from() only for known-valid values.Status::from($request->validated('status')).$this->status->value for storage/API payloads and $this->status->name for human-readable labels. Avoid relying on implicit __toString() unless the package documents it.Post::where('status', Status::Published)->get(). For select inputs, use Status::cases() to build options.Status::Draft) rather than raw strings to keep test data aligned with production types.label()/description() helpers to use interfaces or traits.=== against enum cases; use ->value when emitting data attributes or hidden inputs.composer.json platform config and CI matrices before upgrading. Run composer why-not <package> 1.1 if the upgrade is blocked.string or int backing types.?Status::class casts and handle null explicitly; tryFrom() returns null on failure, which is useful for optional filters.->value or ->name. Mixing them causes subtle frontend bugs. Add a toArray() override or API resource mapping if needed.Rule::enum() is available in newer Laravel versions; if you are on an older minor, use the package's rule or in:. Confirm the package's compatibility matrix.assertSame(Status::Draft, $post->status)) rather than strings. This catches cast regressions early.php artisan config:clear, composer dump-autoload). If casts fail, check that the enum is backed and the column type matches the backing type.HasLabel) on enums to keep presentation logic out of models.How can I help you explore Laravel packages today?