Download the PHP package celinederoland/eloquent-polymorphic-model without Composer
On this page you can find all versions of the php package celinederoland/eloquent-polymorphic-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download celinederoland/eloquent-polymorphic-model
More information about celinederoland/eloquent-polymorphic-model
Files in celinederoland/eloquent-polymorphic-model
Package eloquent-polymorphic-model
Short Description Extend eloquent facilities to map one class Hierarchy branch to one table in database
License
Informations about the package eloquent-polymorphic-model
Eloquent extension for polymorphic models
Extend eloquent facilities to map one class Hierarchy branch to one table in database.
Purpose
If you have one table on database (let say a Person
table with fields person_id
, name
and gender
) and you want to map it with your class hierarchy (let say a parent class Person
and 2 childs Man extends Person
and Woman extends Person
), then you can use this package to make the mapping automatically.
Configure class mapping
Retrieving instances from the parent class
In parent class, define the Model as usual :
Define empty children classes :
In parent class use the EloquentPolymorphism\PolymorphicParent
helper
You must define how database results will be bound with your class hierarchy (in my example it depends on the gender
field value)
With that you can retrieve a collection of Men and Women :
Retrieving instances from children classes
Now we must define constraints in child classes (otherwise Man::all()
would also retrieve a collection of men and women).
For that in children classes you have to use the trait EloquentPolymorphism\PolymorphicChild
and define the polymorphismScope
constraint ;
Now if you write Man::all()
or any more complex query on Man
model it will result on a collection of Man
instances, corresponding to the table entries which represent men.
Optionally, you can overwrite the name of the scope you just defined in Man
class adding this code either in parent or child class :
Updating/Creating model :
default attributes
It is strongly recommended to define default attributes values in children classes.
In our example it would be comfortable to write :
without having to set her gender.
For this purpose, you must overwrite method setChildDefaultAttributes
in children classes
verifications on save method call
The trait PolymorphicParent
prevents unnatural update/create on children like as example :
This is done by checking that the conditions defined in instanceFactory
method would effectively retrieve an instance of Man
.
You can overwrite this behaviour by implementing the method checkHierarchyConstraintsBeforeSaving
Complex queries, relations, etc.
You can use all other functionality of Eloquent models like usual. In particular, you can define relations and complex queries as needed.
Contribute
Fork the project in your github account. Init gitflow
Composer install `
Test
Code
Create a merge request from you're release branch to develop