Download the PHP package franzose/closure-table without Composer
On this page you can find all versions of the php package franzose/closure-table. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download franzose/closure-table
More information about franzose/closure-table
Files in franzose/closure-table
Package closure-table
Short Description Adjacency List’ed Closure Table database design pattern implementation for Laravel
License MIT
Informations about the package closure-table
ClosureTable
This is a database manipulation package for the Laravel 5.4+ framework. You may want to use it when you need to store and operate hierarchical data in your database. The package is an implementation of a well-known design pattern called closure table. However, in order to simplify and optimize SQL SELECT
queries, it uses adjacency lists to query direct parent/child relationships.
Contents:
- Installation
- Setup
- Requirements
- Examples → List of Scopes
- Examples → Parent/Root
- Examples → Ancestors
- Examples → Descendants
- Examples → Children
- Examples → Siblings
- Examples → Tree
- Examples → Collection Methods
Installation
It's strongly recommended to use Composer to install the package:
If you use Laravel 5.5+, the package's service provider is automatically registered for you thanks to the package auto-discovery feature. Otherwise, you have to manually add it to your config/app.php
:
Setup
In a basic scenario, you can simply run the following command:
Where Node
is the name of the entity model. This is what you get from running the above:
- Two models in the
app
directory:App\Node
andApp\NodeClosure
- A new migration in the
database/migrations
directory
As you can see, the command requires a single argument, name of the entity model. However, it accepts several options in order to provide some sort of customization: Option | Alias | Meaning |
---|---|---|
namespace | ns | Custom namespace for generated models. Keep in mind that the given namespace will override model namespaces: php artisan closuretable:make Foo\\Node --namespace=Qux --closure=Bar\\NodeTree will generate Qux\Node and Qux\NodeTree models. |
entity-table | et | Database table name for the entity model |
closure | c | Class name for the closure model |
closure-table | ct | Database table name for the closure model |
models-path | mdl | Directory in which to put generated models |
migrations-path | mgr | Directory in which to put generated migrations |
use-innodb | i | This flag will tell the generator to set database engine to InnoDB. Useful only if you use MySQL |
Requirements
You have to keep in mind that, by design of this package, the models/tables have a required minimum of attributes/columns:
Entity | ||
---|---|---|
Attribute/Column | Customized by | Meaning |
parent_id | Entity::getParentIdColumn() |
ID of the node's immediate parent, simplifies queries for immediate parent/child nodes. |
position | Entity::getPositionColumn() |
Node position, allows to order nodes of the same depth level |
ClosureTable | ||
Attribute/Column | Customized by | Meaning |
id | ||
ancestor | ClosureTable::getAncestorColumn() |
Parent (self, immediate, distant) node ID |
descendant | ClosureTable::getDescendantColumn() |
Child (self, immediate, distant) node ID |
depth | ClosureTable::getDepthColumn() |
Current nesting level, 0+ |
Examples
In the examples, let's assume that we've set up a Node
model which extends the Franzose\ClosureTable\Models\Entity
model.
Scopes
Since ClosureTable 6, a lot of query scopes have become available in the Entity model:
You can learn how to use query scopes from the Laravel documentation.
Parent/Root
Ancestors
There are several methods that have been deprecated since ClosureTable 6:
Descendants
There are several methods that have been deprecated since ClosureTable 6:
Children
Siblings
Tree
There are several methods that have been deprecated since ClosureTable 6:
Collection methods
This library uses an extended collection class which offers some convenient methods: