{"_id":"570dfa90bc6871170034fc0c","user":"570df764b22f0129008dfc23","parentDoc":null,"category":{"_id":"563b6b25e951f60d000b4513","version":"563b65bd9e3f2225009fd2bf","__v":6,"pages":["563c21fd19ae7b0d0050d45b","563c220c7539dd0d00dbee87","563c2218ac77910d00279fe7","563c2233d8f2d20d00448b4f","563c2376913e650d00b65dbd","563c907319ae7b0d0050d528"],"project":"563b65bd9e3f2225009fd2bc","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-11-05T14:43:49.565Z","from_sync":false,"order":1,"slug":"the-basics","title":"The Basics"},"project":"563b65bd9e3f2225009fd2bc","version":{"_id":"563b65bd9e3f2225009fd2bf","project":"563b65bd9e3f2225009fd2bc","__v":4,"createdAt":"2015-11-05T14:20:45.639Z","releaseDate":"2015-11-05T14:20:45.639Z","categories":["563b65be9e3f2225009fd2c0","563b6b25e951f60d000b4513","563c239e260dde0d00c5e890","563c2440260dde0d00c5e891"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"4.0.0","version":"4.0"},"__v":14,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-13T07:51:44.672Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":5,"body":"The model traits can implement functionality. The node in the design can be divided into **Simple Tree** as well as a **Nested Tree**\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Simple Tree\"\n}\n[/block]\nA simple tree model will use the ***parent_id*** column maintain a parent and child relationship between models. To use the simple tree, apply the ***Litepie\\Node\\Traits\\SimpleTree*** trait.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\nclass Category extends Model\\n{\\n    use \\\\Litepie\\\\Node\\\\Traits\\\\SimpleTree;\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThis trait will automatically inject two model relations called **parent** and **children**, it is the equivalent of the following definitions\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\npublic $belongsTo = [\\n    'parent'    => ['User', 'key' => 'parent_id'],\\n];\\n\\npublic $hasMany = [\\n    'children'    => ['User', 'key' => 'parent_id'],\\n];\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nYou can further go onto modifying the respective key name that has been used to identify the parent by defining the **parent_id** variable:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\nvar paretn_id = 'parent_id';\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nCollections of models that use this trait will return the type of **\\Litepie\\Node\\NodeCollection** which adds the **toNested** method. To build an eager loaded tree structure, return the records with the relations eager loaded.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\nCategory::all()->toNested();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Nested Tree\"\n}\n[/block]\nThe nested set model is an advanced technique for maintaining hierachies among models using ***parent_id***, ***nest_left***, ***nest_right***, and ***nest_depth*** columns. To use a nested set model, apply the **\\Litepie\\Node\\Traits\\NestedTree*** trait. All of the features of the ***SimpleTree*** trait are inherently available in this model.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\nclass Category extends Model\\n{\\n    use \\\\Litepie\\\\Node\\\\Traits\\\\NestedNode;\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n##Creating a root node##\n\nThe default action of the compiler is to create all nodes as root nodes.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\n$root = Category::create(['name' => 'Root category']);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nAlternatively, you may find yourself in the need of converting an existing node into a root node:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\n$node->makeRoot();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nYou may also nullify it's ***parent_id*** column which works the same as `makeRoot'.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\n$node->parent_id = null;\\n$node->save();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n##INSERTING NODES##\n\nYou can insert new nodes directly by the relation:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\n$child1 = $root->children()->create(['name' => 'Child 1']);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nYou could further also use the ***makeChildOf*** method for existing nodes:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\n$child2 = Category::create(['name' => 'Child 2']);\\n$child2->makeChildOf($root);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n##Deleting Nodes##\n\nWhen a particular node is deleted with the delete method, all descendants of the node will also be deleted. Note that the delete model events will not be fired for the child models.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\n$child1->delete();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n##Getting the nesting level of a node##\n\nYou can identify the level at which the node is embedded. The ***getLevel*** method will return current nesting level, or depth, of a node.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?\\n\\n// 0 when root\\n$node->getLevel()\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n##Moving nodes around##\n\n\nThere are several methods for moving nodes around. You could implement them through the following code\n\n***moveLeft()***: Find the left sibling and move to the left of it. \n\n***moveRight()***: Find the right sibling and move to the right of it. \n\n***moveBefore($otherNode)***: Move to the node to the left of a defined node \n\n***moveAfter($otherNode)***: Move to the node to the right of a defined node \n\n***makeChildOf($otherNode)***: Make the node a child of a defined node\n\n***makeRoot()***: Make current node a root node.","excerpt":"","slug":"node","type":"basic","title":"Node"}
The model traits can implement functionality. The node in the design can be divided into **Simple Tree** as well as a **Nested Tree** [block:api-header] { "type": "basic", "title": "Simple Tree" } [/block] A simple tree model will use the ***parent_id*** column maintain a parent and child relationship between models. To use the simple tree, apply the ***Litepie\Node\Traits\SimpleTree*** trait. [block:code] { "codes": [ { "code": "<?\n\nclass Category extends Model\n{\n use \\Litepie\\Node\\Traits\\SimpleTree;\n}", "language": "php" } ] } [/block] This trait will automatically inject two model relations called **parent** and **children**, it is the equivalent of the following definitions [block:code] { "codes": [ { "code": "<?\n\npublic $belongsTo = [\n 'parent' => ['User', 'key' => 'parent_id'],\n];\n\npublic $hasMany = [\n 'children' => ['User', 'key' => 'parent_id'],\n];", "language": "php" } ] } [/block] You can further go onto modifying the respective key name that has been used to identify the parent by defining the **parent_id** variable: [block:code] { "codes": [ { "code": "<?\n\nvar paretn_id = 'parent_id';", "language": "php" } ] } [/block] Collections of models that use this trait will return the type of **\Litepie\Node\NodeCollection** which adds the **toNested** method. To build an eager loaded tree structure, return the records with the relations eager loaded. [block:code] { "codes": [ { "code": "<?\n\nCategory::all()->toNested();", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Nested Tree" } [/block] The nested set model is an advanced technique for maintaining hierachies among models using ***parent_id***, ***nest_left***, ***nest_right***, and ***nest_depth*** columns. To use a nested set model, apply the **\Litepie\Node\Traits\NestedTree*** trait. All of the features of the ***SimpleTree*** trait are inherently available in this model. [block:code] { "codes": [ { "code": "<?\n\nclass Category extends Model\n{\n use \\Litepie\\Node\\Traits\\NestedNode;\n}", "language": "php" } ] } [/block] ##Creating a root node## The default action of the compiler is to create all nodes as root nodes. [block:code] { "codes": [ { "code": "<?\n\n$root = Category::create(['name' => 'Root category']);", "language": "php" } ] } [/block] Alternatively, you may find yourself in the need of converting an existing node into a root node: [block:code] { "codes": [ { "code": "<?\n\n$node->makeRoot();", "language": "php" } ] } [/block] You may also nullify it's ***parent_id*** column which works the same as `makeRoot'. [block:code] { "codes": [ { "code": "<?\n\n$node->parent_id = null;\n$node->save();", "language": "php" } ] } [/block] ##INSERTING NODES## You can insert new nodes directly by the relation: [block:code] { "codes": [ { "code": "<?\n\n$child1 = $root->children()->create(['name' => 'Child 1']);", "language": "php" } ] } [/block] You could further also use the ***makeChildOf*** method for existing nodes: [block:code] { "codes": [ { "code": "<?\n\n$child2 = Category::create(['name' => 'Child 2']);\n$child2->makeChildOf($root);", "language": "php" } ] } [/block] ##Deleting Nodes## When a particular node is deleted with the delete method, all descendants of the node will also be deleted. Note that the delete model events will not be fired for the child models. [block:code] { "codes": [ { "code": "<?\n\n$child1->delete();", "language": "php" } ] } [/block] ##Getting the nesting level of a node## You can identify the level at which the node is embedded. The ***getLevel*** method will return current nesting level, or depth, of a node. [block:code] { "codes": [ { "code": "<?\n\n// 0 when root\n$node->getLevel()", "language": "php" } ] } [/block] ##Moving nodes around## There are several methods for moving nodes around. You could implement them through the following code ***moveLeft()***: Find the left sibling and move to the left of it. ***moveRight()***: Find the right sibling and move to the right of it. ***moveBefore($otherNode)***: Move to the node to the left of a defined node ***moveAfter($otherNode)***: Move to the node to the right of a defined node ***makeChildOf($otherNode)***: Make the node a child of a defined node ***makeRoot()***: Make current node a root node.