Download the PHP package paxha/laravel-adjacency-list without Composer
On this page you can find all versions of the php package paxha/laravel-adjacency-list. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download paxha/laravel-adjacency-list
More information about paxha/laravel-adjacency-list
Files in paxha/laravel-adjacency-list
Package laravel-adjacency-list
Short Description Recursive Laravel Eloquent relationships with CTEs
License MIT
Informations about the package laravel-adjacency-list
Introduction
This Laravel Eloquent extension provides recursive relationships using common table expressions (CTE).
Supports Laravel 5.5.29+.
Compatibility
- MySQL 8.0+
- MariaDB 10.2+
- PostgreSQL 9.4+
- SQLite 3.8.3+
- SQL Server 2008+
Installation
composer require paxha/laravel-adjacency-list
Usage
- Getting Started
- Relationships
- Tree
- Filters
- Order
- Depth
- Path
Getting Started
Consider the following table schema for hierarchical data:
Use the HasRecursiveRelationships
trait in your model to work with recursive relationships:
By default, the trait expects a parent key named parent_id
. You can customize it by overriding getParentKeyName()
:
Relationships
The trait provides various relationships:
ancestors()
: The model's recursive parents.ancestorsAndSelf()
: The model's recursive parents and itself.children()
: The model's direct children.childrenAndSelf()
: The model's direct children and itself.descendants()
: The model's recursive children.descendantsAndSelf()
: The model's recursive children and itself.parent()
: The model's direct parent.parentAndSelf()
: The model's direct parent and itself.siblings()
: The parent's other children.siblingsAndSelf()
: All the parent's children.
Tree
The trait provides the tree()
query scope to get all models, beginning at the root(s):
Filters
The trait provides query scopes to filter models by their position in the tree:
hasChildren()
: Models with children.hasParent()
: Models with a parent.isLeaf()
: Models without children.isRoot()
: Models without a parent.
Order
The trait provides query scopes to order models breadth-first or depth-first:
breadthFirst()
: Get siblings before children.depthFirst()
: Get children before siblings.
Depth
The results of ancestor, descendant and tree queries include an additional depth
column.
It contains the model's depth relative to the query's parent. The depth is positive for descendants and negative for ancestors:
You can customize the column name by overriding getDepthName()
:
You can use the whereDepth()
query scope to filter models by their relative depth:
Path
The results of ancestor, descendant and tree queries include an additional path
column.
It contains the dot-separated path of primary keys from the query's parent to the model:
You can customize the column name and the separator by overriding the respective methods:
Contributing
Please see CODE OF CONDUCT for details.