Download the PHP package staudenmeir/laravel-adjacency-list without Composer
On this page you can find all versions of the php package staudenmeir/laravel-adjacency-list. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download staudenmeir/laravel-adjacency-list
More information about staudenmeir/laravel-adjacency-list
Files in staudenmeir/laravel-adjacency-list
Package laravel-adjacency-list
Short Description Recursive Laravel Eloquent relationships with CTEs
License MIT
Informations about the package laravel-adjacency-list
Laravel Adjacency List
This Laravel Eloquent extension provides recursive relationships for trees and graphs using common table expressions (CTE).
Compatibility
- MySQL 8.0+
- MariaDB 10.2+
- PostgreSQL 9.4+
- SQLite 3.8.3+
- SQL Server 2008+
- SingleStore 8.1+ (only trees)
- Firebird
Installation
composer require staudenmeir/laravel-adjacency-list:"^1.0"
Use this command if you are in PowerShell on Windows (e.g. in VS Code):
composer require staudenmeir/laravel-adjacency-list:"^^^^1.0"
Versions
Laravel | Package |
---|---|
11.x | 1.21 |
10.x | 1.13 |
9.x | 1.12 |
8.x | 1.9 |
7.x | 1.5 |
6.x | 1.3 |
5.8 | 1.1 |
5.5–5.7 | 1.0 |
Usage
The package offers recursive relationships for traversing two types of data structures:
- Trees: One Parent per Node (One-to-Many)
- Graphs: Multiple Parents per Node (Many-to-Many)
Trees: One Parent per Node (One-to-Many)
Use the package to traverse a tree structure with one parent per node. Use cases might be recursive categories, a page hierarchy or nested comments.
Supports Laravel 5.5+.
- Getting Started
- Included Relationships
- Trees
- Filters
- Order
- Depth
- Path
- Custom Paths
- Nested Results
- Initial & Recursive Query Constraints
- Additional Methods
- Custom Relationships
- Deep Relationship Concatenation
- Known Issues
Getting Started
Consider the following table schema for hierarchical data in trees:
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()
:
By default, the trait uses the model's primary key as the local key. You can customize it by
overriding getLocalKeyName()
:
Included Relationships
The trait provides various relationships:
ancestors()
: The model's recursive parents.ancestorsAndSelf()
: The model's recursive parents and itself.bloodline()
: The model's ancestors, descendants 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.rootAncestor()
: The model's topmost parent.rootAncestorOrSelf()
: The model's topmost parent or itself.siblings()
: The parent's other children.siblingsAndSelf()
: All the parent's children.
Trees
The trait provides the tree()
query scope to get all models, beginning at the root(s):
treeOf()
allows you to query trees with custom constraints for the root model(s). Consider a table with multiple
separate lists:
You can also pass a maximum depth:
You can also chaperone tree relations to load parent/ancestor relations already present in the tree to (potentially) reduce 1+n queries:
Or via toTree
:
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()
/doesntHaveChildren()
: 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, bloodline, 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:
Change the column name by overriding getDepthName()
if your table already contains a depth
column:
Depth Constraints
You can use the whereDepth()
query scope to filter models by their relative depth:
Queries with whereDepth()
constraints that limit the maximum depth still build the entire (sub)tree internally.
Use withMaxDepth()
to set a maximum depth that improves query performance by only building the requested section of
the tree:
This also works with negative depths (where it's technically a minimum):
Path
The results of ancestor, bloodline, descendant and tree queries include an additional path
column.
It contains the dot-separated path of local keys from the query's parent to the model:
Change the column name by overriding getPathName()
if your table already contains a path
column:
You can also customize the path separator by overriding getPathSeparator()
:
Custom Paths
You can add custom path columns to the query results:
You can also reverse custom paths:
Nested Results
Use the toTree()
method on a result collection to generate a nested tree:
This recursively sets children
relationships:
Initial & Recursive Query Constraints
You can add custom constraints to the CTE's initial and recursive query. Consider a query where you want to traverse a tree while skipping inactive users and their descendants:
You can also add a custom constraint to only the initial or recursive query using withInitialQueryConstraint()
/
withRecursiveQueryConstraint()
.
Additional Methods
The trait also provides methods to check relationships between models:
isChildOf(Model $model)
: Checks if the current model is a child of the given model.isParentOf(Model $model)
: Checks if the current model is a parent of the given model.getDepthRelatedTo(Model $model)
: Returns the depth of the current model related to the given model.
Custom Relationships
You can also define custom relationships to retrieve related models recursively.
- HasManyOfDescendants
- BelongsToManyOfDescendants
- MorphToManyOfDescendants
- MorphedByManyOfDescendants
- Intermediate Scopes
- Usage outside of Laravel
HasManyOfDescendants
Consider a HasMany
relationship between User
and Post
:
Define a HasManyOfDescendants
relationship to get all posts of a user and its descendants:
Use hasManyOfDescendants()
to only get the descendants' posts:
BelongsToManyOfDescendants
Consider a BelongsToMany
relationship between User
and Role
:
Define a BelongsToManyOfDescendants
relationship to get all roles of a user and its descendants:
Use belongsToManyOfDescendants()
to only get the descendants' roles:
MorphToManyOfDescendants
Consider a MorphToMany
relationship between User
and Tag
:
Define a MorphToManyOfDescendants
relationship to get all tags of a user and its descendants:
Use morphToManyOfDescendants()
to only get the descendants' tags:
MorphedByManyOfDescendants
Consider a MorphedByMany
relationship between Category
and Post
:
Define a MorphedByManyOfDescendants
relationship to get all posts of a category and its descendants:
Use morphedByManyOfDescendants()
to only get the descendants' posts:
Intermediate Scopes
You can adjust the descendants query (e.g. child users) by adding or removing intermediate scopes:
Usage outside of Laravel
If you are using the package outside of Laravel or have disabled package discovery for staudenmeir/laravel-cte
, you
need to add support for common table expressions to the related model:
Deep Relationship Concatenation
You can include recursive relationships into deep relationships by concatenating them with other relationships
using staudenmeir/eloquent-has-many-deep. This
works with Ancestors
, Bloodline
and Descendants
relationships (Laravel 9+).
Consider a HasMany
relationship between User
and Post
and building a deep relationship to get all posts of a
user's descendants:
User
→ descendants → User
→ has many → Post
Install the additional package, add the
HasRelationships
trait to the recursive model
and define a
deep relationship:
At the moment, recursive relationships can only be at the beginning of deep relationships:
- Supported:
User
→ descendants →User
→ has many →Post
- Not supported:
Post
→ belongs to →User
→ descendants →User
Known Issues
MariaDB doesn't yet support correlated CTEs in subqueries. This affects
queries like User::whereHas('descendants')
or User::withCount('descendants')
.
Graphs: Multiple Parents per Node (Many-to-Many)
You can also use the package to traverse graphs with multiple parents per node that are defined in a pivot table. Use cases might be a bill of materials (BOM) or a family tree.
Supports Laravel 9+.
- Getting Started
- Included Relationships
- Pivot Columns
- Cycle Detection
- Subgraphs
- Order
- Depth
- Path
- Custom Paths
- Nested Results
- Initial & Recursive Query Constraints
- Deep Relationship Concatenation
- Known Issues
Getting Started
Consider the following table schema for storing directed graphs as nodes and edges:
Use the HasGraphRelationships
trait in your model to work with graph relationships and specify the name of the pivot
table:
By default, the trait expects a parent key named parent_id
and child key named child_id
in the pivot table. You can
customize them by overriding getParentKeyName()
and getChildKeyName()
:
By default, the trait uses the model's primary key as the local key. You can customize it by
overriding getLocalKeyName()
:
Included Relationships
The trait provides various relationships:
ancestors()
: The node's recursive parents.ancestorsAndSelf()
: The node's recursive parents and itself.children()
: The node's direct children.childrenAndSelf()
: The node's direct children and itself.descendants()
: The node's recursive children.descendantsAndSelf()
: The node's recursive children and itself.parents()
: The node's direct parents.parentsAndSelf()
: The node's direct parents and itself.
Pivot Columns
Similar to BelongsToMany
relationships, you can retrieve additional columns from the pivot table besides the parent
and child key:
Cycle Detection
If your graph contains cycles, you need to enable cycle detection to prevent infinite loops:
You can also retrieve the start of a cycle, i.e. the first duplicate node. With this option, the query results include
an is_cycle
column that indicates whether the node is part of a cycle:
Subgraphs
The trait provides the subgraph()
query scope to get the subgraph of a custom constraint:
You can pass a maximum depth as the second argument:
Order
The trait provides query scopes to order nodes breadth-first or depth-first:
breadthFirst()
: Get siblings before children.depthFirst()
: Get children before siblings.
Depth
The results of ancestor, descendant and subgraph queries include an additional depth
column.
It contains the node's depth relative to the query's parent. The depth is positive for descendants and negative for ancestors:
Change the column name by overriding getDepthName()
if your table already contains a depth
column:
Depth Constraints
You can use the whereDepth()
query scope to filter nodes by their relative depth:
Queries with whereDepth()
constraints that limit the maximum depth still build the entire (sub)graph internally.
Use withMaxDepth()
to set a maximum depth that improves query performance by only building the requested section of
the graph:
This also works with negative depths (where it's technically a minimum):
Path
The results of ancestor, descendant and subgraph queries include an additional path
column.
It contains the dot-separated path of local keys from the query's parent to the node:
Change the column name by overriding getPathName()
if your table already contains a path
column:
You can also customize the path separator by overriding getPathSeparator()
:
Custom Paths
You can add custom path columns to the query results:
You can also reverse custom paths:
Nested Results
Use the toTree()
method on a result collection to generate a nested tree:
This recursively sets children
relationships:
Initial & Recursive Query Constraints
You can add custom constraints to the CTE's initial and recursive query. Consider a query where you want to traverse a node's descendants while skipping inactive nodes and their descendants:
You can also add a custom constraint to only the initial or recursive query using withInitialQueryConstraint()
/
withRecursiveQueryConstraint()
.
Deep Relationship Concatenation
You can include recursive relationships into deep relationships by concatenating them with other relationships using staudenmeir/eloquent-has-many-deep (Laravel 9+).
Consider a HasMany
relationship between Node
and Post
and building a deep relationship to get all posts of a
node's descendants:
Node
→ descendants → Node
→ has many → Post
Install the additional package, add the
HasRelationships
trait to the recursive model
and define a
deep relationship:
At the moment, recursive relationships can only be at the beginning of deep relationships:
- Supported:
Node
→ descendants →Node
→ has many →Post
- Not supported:
Post
→ belongs to →Node
→ descendants →Node
Known Issues
MariaDB doesn't yet support correlated CTEs in subqueries. This affects
queries like Node::whereHas('descendants')
or Node::withCount('descendants')
.
Package Conflicts
staudenmeir/eloquent-param-limit-fix
: Replace both packages with staudenmeir/eloquent-param-limit-fix-x-laravel-adjacency-list to use them on the same model.
Contributing
Please see CODE OF CONDUCT for details.
All versions of laravel-adjacency-list with dependencies
illuminate/database Version ^11.0
staudenmeir/eloquent-has-many-deep-contracts Version ^1.2
staudenmeir/laravel-cte Version ^1.11