Download the PHP package srlabs/eloquent-sti without Composer
On this page you can find all versions of the php package srlabs/eloquent-sti. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download srlabs/eloquent-sti
More information about srlabs/eloquent-sti
Files in srlabs/eloquent-sti
Package eloquent-sti
Short Description Implement Single Table Inheritance within the Eloquent ORM
License MIT
Informations about the package eloquent-sti
Eloquent Single Table Inheritance
This package provides an easy way to extend Eloquent model objects to provide support for single table inheritance. Inspired by an article written by Pallav Kaushish.
Installation
This package should be installed via composer:
Usage
In your Model, add the trait and then specify these configuration values:
- : The name of the database table assigned to the base model
- : The full class name of the base Eloquent Object
- : The column in the database table used to distinguish STI entity types
- : An array mapping discriminator column values to corresponding entity classes
Here is an imaginary Widget model as an example:
Next you need to create each of your child entity classes. I often keep them in an folder, but any namespaced location will work.
Here is a hypothetical example:
Whenever Eloquent wants to return an instance of the base model it will use the value of the discriminator column to determine the appropriate entity type and return an instance of that class instead. This holds true for all Eloquent actions but it will not work on direct database (i.e. ) calls.
Providing the method in the child class will allow you to use the Entity as a traditional Eloquent accessor that only returns entities of its own type. In this case, would return all of the widgets flagged as 'new' in the database.
Any questions about this package should be posted on the package website.