Skip to content

Instantly share code, notes, and snippets.

@fayzasalman
Forked from etrepat/traversing-example.md
Created April 8, 2021 15:57
Show Gist options
  • Select an option

  • Save fayzasalman/0f32faa7ebc257c05cedc44c33726025 to your computer and use it in GitHub Desktop.

Select an option

Save fayzasalman/0f32faa7ebc257c05cedc44c33726025 to your computer and use it in GitHub Desktop.

Revisions

  1. @etrepat etrepat renamed this gist Oct 10, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @etrepat etrepat revised this gist Oct 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion traversing.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ foreach($root->children as $category) {
    }
    ```

    This approach is ok if you don't need to go too deep under your hierarchy tree (usually 1 to 3 levels). If you'd need to traverse your tree deeper or, maybe get the full hierarchy tree, a recursive approach should be better.
    This approach is ok if you don't need to go too deep under your hierarchy tree (usually 1 to 3 levels). If you'd need to traverse your tree deeper or, maybe get the full hierarchy tree, you could consider a simple recursive approach.

    ```PHP
    <?php
  3. @etrepat etrepat revised this gist Oct 10, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions traversing.md
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,8 @@ foreach($root->children as $category) {
    This approach is ok if you don't need to go too deep under your hierarchy tree (usually 1 to 3 levels). If you'd need to traverse your tree deeper or, maybe get the full hierarchy tree, a recursive approach should be better.

    ```PHP
    <?php

    $roots = Category::roots()->get();

    echo "<ul>";
  4. @etrepat etrepat revised this gist Oct 10, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions traversing.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    The simplest way to traverse the hierarchy tree with [Baum](https://github.com/etrepat/Baum) is by iterating through the `children` relation.

    ```PHP
    <?php

    $root = Category::roots()->with('children')->first();

    echo "<h3>{$root->name}</h3>";
  5. @etrepat etrepat renamed this gist Oct 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md → traversing.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ foreach($root->children as $category) {

    This approach is ok if you don't need to go too deep under your hierarchy tree (usually 1 to 3 levels). If you'd need to traverse your tree deeper or, maybe get the full hierarchy tree, a recursive approach should be better.

    ```php
    ```PHP
    $roots = Category::roots()->get();

    echo "<ul>";
  6. @etrepat etrepat revised this gist Oct 10, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@

    The simplest way to traverse the hierarchy tree with [Baum](https://github.com/etrepat/Baum) is by iterating through the `children` relation.

    ```php
    ```PHP
    $root = Category::roots()->with('children')->first();

    echo "<h3>{$root->name}</h3>";
  7. @etrepat etrepat created this gist Oct 10, 2013.
    47 changes: 47 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@

    The simplest way to traverse the hierarchy tree with [Baum](https://github.com/etrepat/Baum) is by iterating through the `children` relation.

    ```php
    $root = Category::roots()->with('children')->first();

    echo "<h3>{$root->name}</h3>";

    foreach($root->children as $category) {
    echo "<li>";
    echo "<b>{$category->name}</b>";

    if ( $category->children()->count() > 0 ) {
    echo "<ul>";
    foreach($category->children as $subCategory)
    echo "<li>{$subCategory}</li>";
    echo "</ul>";
    }

    echo "</li>";
    }
    ```

    This approach is ok if you don't need to go too deep under your hierarchy tree (usually 1 to 3 levels). If you'd need to traverse your tree deeper or, maybe get the full hierarchy tree, a recursive approach should be better.

    ```php
    $roots = Category::roots()->get();

    echo "<ul>";
    foreach($roots as $root) renderNode($root);
    echo "</ul>";

    // *Very simple* recursive rendering function
    function renderNode($node) {
    echo "<li>";
    echo "<b>{$node->name}</b>";

    if ( $node->children()->count() > 0 ) {
    echo "<ul>";
    foreach($node->children as $child) renderNode($child);
    echo "</ul>";
    }

    echo "</li>";

    }
    ```