SimpleTree
Simple Tree model trait
trait Winter\Storm\Database\Traits\SimpleTree
Simple tree implementation, for advanced implementation see: Winter\Storm\Database\Traits\NestedTree
SimpleTree is the bare minimum needed for tree functionality, the methods defined here should be implemented by all "tree" traits.
Usage:
Model table must have parent_id table column. In the model class definition:
use \Winter\Storm\Database\Traits\SimpleTree;
General access methods:
$model->getChildren(); // Returns children of this node $model->getChildCount(); // Returns number of all children. $model->getAllChildren(); // Returns all children of this node $model->getAllRoot(); // Returns all root level nodes (eager loaded) $model->getAll(); // Returns everything in correct order.
Query builder methods:
$query->listsNested(); // Returns an indented array of key and value columns.
You can change the sort field used by declaring:
const PARENT_ID = 'my_parent_column';
Methods
public static bootSimpleTree ()
public getAll () : Winter\Storm\Database\Collection
public getAllChildren () : Winter\Storm\Database\Collection
Get a list of children records, with their children (recursive)
public getChildCount () : int
Returns number of all children below it.
public getChildren () : Winter\Storm\Database\Collection
public getParentColumnName () : string
Get parent column name.
public getParentId () : int
Get value of the model parent_id column.
public getQualifiedParentColumnName () : string
Get fully qualified parent column name.
public
newCollection (array $models = []
)
Return a custom TreeCollection collection
Property | Type | Description |
---|---|---|
$models | array |
array
|
public scopeGetAllRoot ($query) : Winter\Storm\Database\Collection
Returns a list of all root nodes, without eager loading.
Property | Type | Description |
---|---|---|
$query | mixed |
mixed
|
public scopeGetNested ($query) : Collection
Non chaining scope, returns an eager loaded hierarchy tree. Children are eager loaded inside the $model->children relation.
Property | Type | Description |
---|---|---|
$query | mixed |
mixed
|
A collection
public
scopeListsNested ($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.
Property | Type | Description |
---|---|---|
$query | mixed |
mixed
|
$column | string |
string
Array values |
$key | string |
string
Array keys |
$indent | string |
string
Character to indent depth |