Download the PHP package devlop/laravel-nestedset without Composer

On this page you can find all versions of the php package devlop/laravel-nestedset. 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 laravel-nestedset

This is a fork of lazychaser/nestedset.

The purpose of this fork is to fix some critical bugs, legacy support is dropped and only Laravel 8 is supported.

If the bugs are fixed in lazychaser/nestedset this fork will be archived.

Contents:

What are nested sets?

Nested sets or Nested Set Model is a way to effectively store hierarchical data in a relational table. From wikipedia:

The nested set model is to number the nodes according to a tree traversal, which visits each node twice, assigning numbers in the order of visiting, and at both visits. This leaves two numbers for each node, which are stored as two attributes. Querying becomes inexpensive: hierarchy membership can be tested by comparing these numbers. Updating requires renumbering and is therefore expensive.

Applications

NSM shows good performance when tree is updated rarely. It is tuned to be fast for getting related nodes. It'is ideally suited for building multi-depth menu or categories for shop.

Documentation

Suppose that we have a model Category; a $node variable is an instance of that model and the node that we are manipulating. It can be a fresh model or one from database.

Relationships

Node has following relationships that are fully functional and can be eagerly loaded:

Inserting nodes

Moving and inserting nodes includes several database queries, so it is highly recommended to use transactions.

IMPORTANT! As of v4.2.0 transaction is not automatically started

Another important note is that structural manipulations are deferred until you hit save on model (some methods implicitly call save and return boolean result of the operation).

If model is successfully saved it doesn't mean that node was moved. If your application depends on whether the node has actually changed its position, use hasMoved method:

Creating nodes

When you simply creating a node, it will be appended to the end of the tree:

In this case the node is considered a root which means that it doesn't have a parent.

Making a root from existing node

The node will be appended to the end of the tree.

Appending and prepending to the specified parent

If you want to make node a child of other node, you can make it last or first child.

In following examples, $parent is some existing node.

There are few ways to append a node:

And only a couple ways to prepend:

Inserting before or after specified node

You can make $node to be a neighbor of the $neighbor node using following methods:

$neighbor must exists, target node can be fresh. If target node exists, it will be moved to the new position and parent will be changed if it's required.

Building a tree from array

When using static method create on node, it checks whether attributes contains children key. If it does, it creates more nodes recursively.

$node->children now contains a list of created child nodes.

Rebuilding a tree from array

You can easily rebuild a tree. This is useful for mass-changing the structure of the tree.

$data is an array of nodes:

There is an id specified for node with the name of foo which means that existing node will be filled and saved. If node is not exists ModelNotFoundException is thrown. Also, this node has children specified which is also an array of nodes; they will be processed in the same manner and saved as children of node foo.

Node bar has no primary key specified, so it will be created.

$delete shows whether to delete nodes that are already exists but not present in $data. By default, nodes aren't deleted.

Rebuilding a subtree

As of 4.2.8 you can rebuild a subtree:

This constraints tree rebuilding to descendants of $root node.

Retrieving nodes

In some cases we will use an $id variable which is an id of the target node.

Ancestors and descendants

Ancestors make a chain of parents to the node. Helpful for displaying breadcrumbs to the current category.

Descendants are all nodes in a sub tree, i.e. children of node, children of children, etc.

Both ancestors and descendants can be eagerly loaded.

It is possible to load ancestors and descendants using custom query:

In most cases, you need your ancestors to be ordered by the level:

A collection of ancestors can be eagerly loaded:

Siblings

Siblings are nodes that have same parent.

To get only next siblings:

To get previous siblings:

Getting related models from other table

Imagine that each category has many goods. I.e. HasMany relationship is established. How can you get all goods of $category and every its descendant? Easy!

Including node depth

If you need to know at which level the node is:

Root node will be at level 0. Children of root nodes will have a level of 1, etc.

To get nodes of specified level, you can apply having constraint:

IMPORTANT! This will not work in database strict mode

Default order

All nodes are strictly organized internally. By default, no order is applied, so nodes may appear in random order and this doesn't affect displaying a tree. You can order nodes by alphabet or other index.

But in some cases hierarchical order is essential. It is required for retrieving ancestors and can be used to order menu items.

To apply tree order defaultOrder method is used:

You can get nodes in reversed order:

To shift node up or down inside parent to affect default order:

The result of the operation is boolean value of whether the node has changed its position.

Constraints

Various constraints that can be applied to the query builder:

Descendants constraints:

Ancestor constraints:

$node can be either a primary key of the model or model instance.

Building a tree

After getting a set of nodes, you can convert it to tree. For example:

This will fill parent and children relationships on every node in the set and you can render a tree using recursive algorithm:

This will output something like this:

Building flat tree

Also, you can build a flat tree: a list of nodes where child nodes are immediately after parent node. This is helpful when you get nodes with custom order (i.e. alphabetically) and don't want to use recursion to iterate over your nodes.

Previous example will output:

Getting a subtree

Sometimes you don't need whole tree to be loaded and just some subtree of specific node. It is show in following example:

In a single query we are getting a root of a subtree and all of its descendants that are accessible via children relation.

If you don't need $root node itself, do following instead:

Deleting nodes

To delete a node:

IMPORTANT! Any descendant that node has will also be deleted!

IMPORTANT! Nodes are required to be deleted as models, don't try do delete them using a query like so:

This will break the tree!

SoftDeletes trait is supported, also on model level.

Helper methods

To check if node is a descendant of other node:

To check whether the node is a root:

Other checks:

Checking consistency

You can check whether a tree is broken (i.e. has some structural errors):

It is possible to get error statistics:

It will return an array with following keys:

Fixing tree

Since v3.1 tree can now be fixed. Using inheritance info from parent_id column, proper _lft and _rgt values are set for every node.

Scoping

Imagine you have Menu model and MenuItems. There is a one-to-many relationship set up between these models. MenuItem has menu_id attribute for joining models together. MenuItem incorporates nested sets. It is obvious that you would want to process each tree separately based on menu_id attribute. In order to do so, you need to specify this attribute as scope attribute:

But now, in order to execute some custom query, you need to provide attributes that are used for scoping:

When requesting nodes using model instance, scopes applied automatically based on the attributes of that model:

To get scoped query builder using instance:

Scoping and eager loading

Always use scoped query when eager loading:

Requirements

It is highly suggested to use database that supports transactions (like MySql's InnoDb) to secure a tree from possible corruption.

Installation

To install the package, in terminal:

Setting up from scratch

The schema

For Laravel 5.5 and above users:

For prior Laravel versions:

To drop columns:

The model

Your model should use Kalnoy\Nestedset\NodeTrait trait to enable nested sets:

Migrating existing data

Migrating from other nested set extension

If your previous extension used different set of columns, you just need to override following methods on your model class:

Migrating from basic parentage info

If your tree contains parent_id info, you need to add two columns to your schema:

After setting up your model you only need to fix the tree to fill _lft and _rgt columns:

License

Copyright (c) 2017 Alexander Kalnoy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


All versions of laravel-nestedset with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.3
illuminate/support Version ~5.7.0|~5.8.0|^6.0|^7.0|^8.0
illuminate/database Version ~5.7.0|~5.8.0|^6.0|^7.0|^8.0
illuminate/events Version ~5.7.0|~5.8.0|^6.0|^7.0|^8.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 devlop/laravel-nestedset contains the following files

Loading the files please wait ....