Download the PHP package baril/bonsai without Composer

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

Bonsai

This package is an implementation of the "Closure Table" design pattern for Laravel and MySQL. This pattern allows for faster querying of tree-like structures stored in a relational database. It is an alternative to nested sets.

Version compatibility

Laravel Bonsai
5.6+ use Smoothie instead
6.x 1.x
7.x 1.x
8.x 2.x / 3.x
9.x 3.x
10.x 3.1+

Closure Table pattern

Let's say you have a tags table that contains a hierarchical list of tags. You probably have a self-referencing foreign key called parent_id or something similar.

The Closure Table pattern says you will create a secondary table (let's call it tag_tree) with the following columns:

The table contains all possible combinations of an ancestor and a descendant. For example, the following tree:

will produce the following closures:

ancestor_id descendant_id depth
1 1 0
1 2 1
1 3 2
1 4 2
1 5 1
2 2 0
2 3 1
2 4 1
3 3 0
4 4 0
5 5 0

Setup

New install

First, your main table needs a parent_id column (the name can be configured). This column is the one that holds the canonical data: the closures are merely a duplication of that information.

Then, have your model implement the Baril\Bonsai\Concerns\BelongsToTree trait.

You can use the following properties to configure the table and column names:

The bonsai:grow command will generate the migration file for the closure table based on your model configuration:

If you use the --migrate option, the command will also run the migration. If your main table already contains data, it will also insert the closures for the existing data.

:warning: If you use the --migrate option, any other pending migrations will run too.

There are some additional options: use --help to learn more.

Artisan commands

In addition to the bonsai:grow command described above, this package provides the following commands:

In case your data gets corrupt somehow, the bonsai:fix command will truncate the closure table and fill it again (based on the data found in the main table's parent_id column):

The bonsai:show command provides a quick-and-easy way to output the content of the tree. It takes a label parameter that defines which column (or accessor) to use as label. Optionally you can also specify a max depth.

Basic usage

Just fill the model's parent_id and save the model: the closure table will be updated accordingly.

The save method will throw a \Baril\Bonsai\TreeException in case of a redundancy error (ie. if the parent_id corresponds to the model itself or one of its descendants).

When you delete a model, its closures will be automatically deleted. If the model has descendants, the delete method will throw a TreeException. You need to use one of these 2 methods instead:

Relationships

The trait defines the following relationships:

:warning: The ancestors and descendants relations are read-only! Trying to use the attach or detach method on them will throw an exception.

The ancestors and descendants relations have the following methods:

Loading or eager-loading the descendants relation will automatically load the children relation (with no additional query). Furthermore, it will load the children relation recursively for all the eager-loaded descendants:

Of course, same goes with the ancestors and parent relations.

Methods

The trait defines the following methods:

Also, the getTree static method can be used to retrieve the whole tree:

It will return a collection of the root elements, with the children relation eager-loaded on every element up to the leafs.

Query scopes

Ordered tree

In case you need each level of the tree to be explicitely ordered, you can use the Baril\Bonsai\Concerns\BelongsToOrderedTree trait (instead of BelongsToTree). In order to use this, you need the Orderly package in addition to Bonsai:

You will need a position column in your main table (the name of the column can be configured with the $orderColumn property).

The children relation will now be ordered. In case you need to order it by some other field, you need to use the unordered scope first:

Also, all methods defined by the Orderable trait described in the Orderly package documentation will now be available:


All versions of bonsai with dependencies

PHP Build Version
Package Version
Requires illuminate/database Version ^8.0|^9.0|^10.0
illuminate/support Version ^8.0|^9.0|^10.0
illuminate/console 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 baril/bonsai contains the following files

Loading the files please wait ....