NestedTree

Nested set model trait

 trait Winter\Storm\Database\Traits\NestedTree

Model table must have parent_id, nest_left, nest_right and nest_depth table columns. In the model class definition:

use \Winter\Storm\Database\Traits\NestedTree;

$table->integer('parent_id')->nullable(); $table->integer('nest_left')->nullable(); $table->integer('nest_right')->nullable(); $table->integer('nest_depth')->nullable();

You can change the column names used by declaring:

const PARENT_ID = 'my_parent_column'; const NEST_LEFT = 'my_left_column'; const NEST_RIGHT = 'my_right_column'; const NEST_DEPTH = 'my_depth_column';

General access methods:

$model->getRoot(); // Returns the highest parent of a node. $model->getRootList(); // Returns an indented array of key and value columns from root. $model->getParent(); // The direct parent node. $model->getParents(); // Returns all parents up the tree. $model->getParentsAndSelf(); // Returns all parents up the tree and self. $model->getChildren(); // Set of all direct child nodes. $model->getSiblings(); // Return all siblings (parent's children). $model->getSiblingsAndSelf(); // Return all siblings and self. $model->getLeaves(); // Returns all final nodes without children. $model->getDepth(); // Returns the depth of a current node. $model->getChildCount(); // Returns number of all children.

Query builder methods:

$query->withoutNode(); // Filters a specific node from the results. $query->withoutSelf(); // Filters current node from the results. $query->withoutRoot(); // Filters root from the results. $query->children(); // Filters as direct children down the tree. $query->allChildren(); // Filters as all children down the tree. $query->parent(); // Filters as direct parent up the tree. $query->parents(); // Filters as all parents up the tree. $query->siblings(); // Filters as all siblings (parent's children). $query->leaves(); // Filters as all final nodes without children. $query->getNested(); // Returns an eager loaded collection of results. $query->listsNested(); // Returns an indented array of key and value columns.

Flat result access methods:

$model->getAll(); // Returns everything in correct order. $model->getAllRoot(); // Returns all root nodes. $model->getAllChildren(); // Returns all children down the tree. $model->getAllChildrenAndSelf(); // Returns all children and self.

Eager loaded access methods:

$model->getEagerRoot(); // Returns a list of all root nodes, with ->children eager loaded. $model->getEagerChildren(); // Returns direct child nodes, with ->children eager loaded.

Properties

protected $moveToNewParentId : int

Indicates if the model should be aligned to new parent.

Methods

public static bootNestedTree ()

Returns
mixed

public deleteDescendants () : void

Deletes a branch off the tree, shifting all the elements on the right back to the left so the counts work.

Returns
void

public getAll (array $columns = ["*"]) : Winter\Storm\Database\Collection

Returns all nodes and children.

Parameters
Property Description
$columns
array
Returns

public getAllChildren () : Winter\Storm\Database\Collection

Returns all children down the tree.

Returns

public getAllChildrenAndSelf () : Winter\Storm\Database\Collection

Returns all children and self.

Returns

public getChildCount () : int

Returns number of all children below it.

Returns
int

public getChildren () : Winter\Storm\Database\Collection

Returns direct child nodes.

Returns

public getDepth () : int

Get value of the depth column.

Returns
int

public getDepthColumnName () : string

Get depth column name.

Returns
string

public getEagerChildren () : Winter\Storm\Database\Collection

Returns direct child nodes, with ->children eager loaded.

Returns

public getEagerRoot () : Winter\Storm\Database\Collection

Returns a list of all root nodes, with children eager loaded.

Returns

public getLeaves () : Winter\Storm\Database\Collection

Returns all final nodes without children.

Returns

public getLeft () : int

Get value of the left column.

Returns
int

public getLeftColumnName () : string

Get left column name.

Returns
string

public getLeftSibling () : Winter\Storm\Database\Model

Return left sibling

Returns

public getLevel () : int

Returns the level of this node in the tree.

Root level is 0.

Returns
int

public getParent () : Winter\Storm\Database\Model

The direct parent node.

Returns

public getParentColumnName () : string

Get parent column name.

Returns
string

public getParentId () : int

Get value of the model parent_id column.

Returns
int

public getParents () : Winter\Storm\Database\Collection

Returns all parents up the tree.

Returns

public getParentsAndSelf () : Winter\Storm\Database\Collection

Returns all parents up the tree and self.

Returns

public getQualifiedDepthColumnName () : string

Get fully qualified depth column name.

Returns
string

public getQualifiedLeftColumnName () : string

Get fully qualified left column name.

Returns
string

public getQualifiedParentColumnName () : string

Get fully qualified parent column name.

Returns
string

public getQualifiedRightColumnName () : string

Get fully qualified right column name.

Returns
string

public getRight () : int

Get value of the right column.

Returns
int

public getRightColumnName () : string

Get right column name.

Returns
string

public getRightSibling () : Winter\Storm\Database\Model

Return right sibling

Returns

public getRoot () : Winter\Storm\Database\Model

Returns the root node starting from the current node.

Returns

public getRootList ($column, $key = null, string $indent = "   ") : array

Returns an array column/key pair of all root nodes, with children eager loaded.

Parameters
Property Description
$column
mixed
$key
mixed
$indent
string
Returns
array

public getSiblings () : Winter\Storm\Database\Collection

Return all siblings (parent's children).

Returns

public getSiblingsAndSelf () : Winter\Storm\Database\Collection

Return all siblings and self.

Returns

public isChild () : bool

Returns true if this is a child node.

Returns
bool

public isDescendantOf ($other) : bool

Returns true if node is a descendant.

Parameters
Property Description
$other
mixed
Returns
bool

public isInsideSubtree (Winter\Storm\Database\Model $node) : bool

Checks if the supplied node is inside the subtree of this model.

Parameters
Returns
bool

public isLeaf () : bool

Returns true if this is a leaf node (end of a branch).

Returns
bool

public isRoot () : bool

Returns true if this is a root node.

Returns
bool

public makeChildOf ($node) : Winter\Storm\Database\Model

Make model node a child of specified node.

Parameters
Property Description
$node
mixed
Returns

public makeRoot () : Winter\Storm\Database\Model

Make this model a root node.

Returns

public moveAfter ($node) : Winter\Storm\Database\Model

Move to the model to after (right) a specified node.

Parameters
Property Description
$node
mixed
Returns

public moveBefore ($node) : Winter\Storm\Database\Model

Move to the model to before (left) specified node.

Parameters
Property Description
$node
mixed
Returns

public moveLeft () : Winter\Storm\Database\Model

Find the left sibling and move to left of it.

Returns

public moveRight () : Winter\Storm\Database\Model

Find the right sibling and move to the right of it.

Returns

public moveToNewParent () : void

If the parent identifier is dirty, realign the nesting.

Returns
void

public newCollection (array $models = []) : TreeCollection

Return a custom TreeCollection collection

Parameters
Property Description
$models
array
Returns

public restoreDescendants () : void

Restores all of the current node descendants.

Returns
void

public scopeAllChildren (Illuminate\Database\Query\Builder $query, boolean $includeSelf = false) : Illuminate\Database\Query\Builder

Set of all children & nested children.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
$includeSelf
boolean
Returns
Illuminate\Database\Query\Builder

public scopeGetAllRoot (Illuminate\Database\Query\Builder $query) : Winter\Storm\Database\Collection

Returns a list of all root nodes, without eager loading

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
Returns

public scopeGetNested (Illuminate\Database\Query\Builder $query) : Collection

Non chaining scope, returns an eager loaded hierarchy tree. Children are eager loaded inside the $model->children relation.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
Returns

A collection

public scopeLeaves (Illuminate\Database\Query\Builder $query) : Illuminate\Database\Query\Builder

Returns all final nodes without children.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
Returns
Illuminate\Database\Query\Builder

public scopeListsNested (Illuminate\Database\Query\Builder $query, string $column, string $key = null, string $indent = "   ") : array

Gets an array with values of a given column. Values are indented according to their depth.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
$column
string

Array values

$key
string

Array keys

$indent
string

Character to indent depth

Returns
array

public scopeParents (Illuminate\Database\Query\Builder $query, boolean $includeSelf = false) : Illuminate\Database\Eloquent\Builder

Returns a prepared query with all parents up the tree.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
$includeSelf
boolean
Returns
Illuminate\Database\Eloquent\Builder

public scopeSiblings (Illuminate\Database\Query\Builder $query, boolean $includeSelf = false) : Illuminate\Database\Eloquent\Builder

Filter targeting all children of the parent, except self.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
$includeSelf
boolean
Returns
Illuminate\Database\Eloquent\Builder

public scopeWithoutNode (Illuminate\Database\Query\Builder $query, $node) : Illuminate\Database\Query\Builder

Query scope which extracts a certain node object from the current query expression.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
$node
mixed
Returns
Illuminate\Database\Query\Builder

public scopeWithoutRoot (Illuminate\Database\Query\Builder $query) : Illuminate\Database\Query\Builder

Extracts first root (from the current node context) from current query expression.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
Returns
Illuminate\Database\Query\Builder

public scopeWithoutSelf (Illuminate\Database\Query\Builder $query) : Illuminate\Database\Query\Builder

Extracts current node (self) from current query expression.

Parameters
Property Description
$query
Illuminate\Database\Query\Builder
Returns
Illuminate\Database\Query\Builder

public setDefaultLeftAndRight () : void

Set defaults for left and right columns.

Returns
void

public setDepth () : Winter\Storm\Database\Model

Sets the depth attribute

Returns

public shiftSiblingsForRestore () : void

Allocates a slot for the the current node between its siblings.

Returns
void

public storeNewParent () : void

Handle if the parent column is modified so it can be realigned.

Returns
void

protected getOtherBoundary (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : int

Calculates the other boundary.

Parameters
Property Description
$node

The node to be moved

$target

The target node to be moved relative to

$position
string

One of the following values: child, left, right

Returns
int

protected getPrimaryBoundary (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : int | null

Calculates the boundary.

Parameters
Property Description
$node

The node to be moved

$target

The target node to be moved relative to

$position
string

One of the following values: child, left, right

Returns
int | null

protected getSortedBoundaries (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : array

Calculates a sorted boundaries array.

Parameters
Property Description
$node

The node to be moved

$target

The target node to be moved relative to

$position
string

One of the following values: child, left, right

Returns
array

protected moveTo (mixed $target, string $position) : Winter\Storm\Database\Model

Handler for all node alignments.

Parameters
Property Description
$target
mixed

The ID or model instance of the target node

$position
string

One of the following values: child, left, right

Returns

protected performMove (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : int

Executes the SQL query associated with the update of the indexes affected by the move operation.

Parameters
Property Description
$node

The node to be moved

$target

The target node to be moved relative to

$position
string

One of the following values: child, left, right

Returns
int

protected validateMove (Winter\Storm\Database\Model $node, Winter\Storm\Database\Model $target, string $position) : bool

Validates a proposed move and returns true if changes are needed.

Parameters
Property Description
$node

The node to be moved

$target

The target node to be moved relative to

$position
string

One of the following values: child, left, right

Returns
bool
Copyright © 2024 Winter CMS