Download the PHP package vicklr/materialized-model without Composer

On this page you can find all versions of the php package vicklr/materialized-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 materialized-model

A solution for parent-child tree structures in Laravel

Latest Stable Version GitHub Tests Action Status Total Downloads

This Laravel package adds hierarchical functionality to your models.

Materialized Model

Materized Model is an implementation of the Materialized Paths pattern for Laravel's Eloquent ORM.

Documentation

About Materialized Paths

The Materialized Paths pattern is a way to have a tree hierarchy of nodes In addition to the node data, it also stores the id(s) of the node’s ancestors or path as a string. Although the Materialized Paths pattern requires additional steps of working with strings and regular expressions, the pattern also provides more flexibility in working with the path, such as finding nodes by partial paths.

For example, you can fetch all descendants of a node in a single query, no matter how deep the tree. The drawback is that insertions/moves/deletes require additional operations, but that is handled behind the scenes by this package.

Materialized Paths are appropriate for ordered trees (e.g. menus, commercial categories, folder structures) and big trees that must be queried efficiently (e.g. threaded posts).

Installation

Materialized Model works with Laravel 8 onwards. You can add it to your project by running:

Getting started

After the package is correctly installed, it can be applied to your models.

Model configuration

In order to work with Materialized Model, you must ensure that your model class uses Vicklr\MaterializedModel\Traits\HasMaterializedPaths or Vicklr\MaterializedModel\Traits\HasOrderedMaterializedPaths.

This is the easiest it can get:

This is a slightly more complex example where we have the column names customized. In order to do so we need to inherit from a base class that uses the trait - such a base class is included in the package, but you can supply your own, as long as it uses the trait as described above:

Remember that, obviously, the column names must match those in the database table.

Migration configuration

You must ensure that the database table that supports your Materialized Models has the following columns:

For that, we have two helper macros on the Blueprint: materializedFields() and materializedOrdering()

The materializedFields() helper will set up the necessary fields for the hierarchy, while materializedOrdering adds the numerical ordering field. If you do not want numerical ordering, define the field yourself and remember to set it as orderColumn on the model

Here is a sample migration file:

You may freely modify the column names, but remember to also change them in the model.

Usage

After you've configured your model and run the migration, you are now ready to use MaterializedModel with your model. Below are some examples.

Creating a root node

By default, all nodes are created as roots:

Alternatively, you may find yourself in the need of converting an existing node into a root node:

Inserting nodes

Deleting nodes

Descendants of deleted nodes will also be deleted due to a foreign key constraint in the database. Please note that, for now, deleting and deleted model events for the descendants will NOT be fired.

Moving nodes around

Materialized Model provides several methods for moving nodes around:

For example:

Asking questions to your nodes

You can ask some questions to your Materialized Model nodes:

Using the nodes from the previous example:

Relations

Materialized Model provides two self-referential Eloquent relations for your nodes: parent and children.

Root scopes

Materialized Model provides some very basic query scopes for accessing the root nodes:

You may also be interested in only the first root:

Accessing the ancestry/descendancy chain

There are several methods which Materialized Model offers to access the ancestry/descendancy chain of a node in the tree. The main thing to keep in mind is that they are provided in two ways:

First as query scopes, returning an Illuminate\Database\Eloquent\Builder instance to continue to query further. To get actual results from these, remember to call get() or first().

Second, as methods which return actual instances (inside a Collection object where appropiate):

Here's a simple example for iterating a node's descendants (provided a name attribute is available):

Limiting the levels of children returned

In some situations where the hierarchy depth is huge it might be desirable to limit the number of levels of children returned (depth). You can do this in Materialized Model by using the limitDepth query scope.

The following snippet will get the current node's descendants up to a maximum of 5 depth levels below it:

Similarly, you can limit the descendancy levels with both the getDescendants and getDescendantsAndSelf methods by supplying the desired depth limit as the first argument:

Custom sorting column

In Materialized Model all results are returned sorted by the ordering column, you specify in your model

Dumping the hierarchy tree

Materialized Model includes the HierarchyCollection that extends the default Eloquent\Collection class and provides the toHierarchy method to it which returns a nested collection representing the queried tree.

Retrieving a complete tree hierarchy into a regular Collection object with its children properly nested is as simple as:

Tree operations on a collection

Materialized Model's HierarchyCollection can be instantiated with a collection of nodes and operations can be run against them, as long as the class name is set on the HierarchyCollection

The following operations are available on the HierarchyCollection:

Model events: MaterializedModelMovedEvent

Materialized Model models dispatches a MaterializedModelMovedEvent whenever a model has moved in the hierarchy.

This event can be acted on by a Listener that can retrieve the moved model and its previous parent, if applicable, from the event.

Tree rebuilding

Materialized Model supports rebuilding (or recalculating the paths) of a model and its children via the rebuild() method.

This method will re-index all your path and depth column values, inspecting your tree only from the parent <-> children relation standpoint. Which means that you only need a correctly filled parent_id column and Materialized Model will try its best to recompute the rest.

This can prove quite useful when something has gone horribly wrong with the index values or it may come quite handy when converting from another implementation (which would probably have a parent_id column).

Simple example usage, given a Category node class:

Soft deletes

Materialized Model does not handle soft deletes specifically, although it should function as long as the parent of a restored node is not soft deleted.

Misc/Utility functions

Node extraction query scopes

Materialized Model provides some query scopes which may be used to extract (remove) selected nodes from the current results set.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of materialized-model with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/support Version ^8.0|^9.0|^10.0
illuminate/database Version ^8.0|^9.0|^10.0
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 vicklr/materialized-model contains the following files

Loading the files please wait ....