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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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


All versions of eloquent-polymorphic-model with dependencies

PHP Build Version
Package Version
Requires php Version 7.*
ext-pdo Version *
ext-json Version *
illuminate/database Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package celinederoland/eloquent-polymorphic-model contains the following files

Loading the files please wait ....