Download the PHP package wfeller/parental without Composer
On this page you can find all versions of the php package wfeller/parental. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wfeller/parental
More information about wfeller/parental
Files in wfeller/parental
Package parental
Short Description A simple eloquent trait that allows relationships to be accessed through child models.
License MIT
Informations about the package parental
Parental
Parental is a Laravel package, forked from calebporzio/parental, 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.
Licence
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
You can buy trees here offset.earth/treeware
Read more about Treeware at treeware.earth
Installation
Each time you add or remove child classes, you'll want to do the following:
This artisan command will simplify the following:
- Laravel Nova resource inheritance (when children don't already have a dedicated Nova resource)
- Adding child global scopes when querying parent
Simple Usage
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
.
Accessing Child Models from Parents
What problem did we just solve?
Before, if we ran: User::first()
we would only get back User
models. By adding the HasChildren
trait and a type
column to the users
table, running User::first()
will return an instance of the child model (Admin
or Guest
in this case).
Type Aliases
If you don't want to store raw class names in the type column, you can override them using the $childTypes
property.
Now, running Admin::create()
will set the type
column in the users
table to admin
instead of App\Admin
.
This feature is useful if you are working with an existing type column, or if you want to decouple application details from your database.
Custom Type Column Name
You can override the default type column by setting the $childColumn
property on the parent model.