Let me answer my own question..lol. as i finally able to get this to work...woohoo .also to help those that have the same problem as me in the future.
- Add a file called helpers.php to your app/ directory. Here i called it as helpers.php.
?php
function renderNode($node) {
if( $node->isLeaf() ) {
return '<li>' . $node->name . '</li>';
} else {
$html = '<li>' . $node->name;
$html .= '<ul>';
foreach($node->children as $child)
$html .= renderNode($child);
$html .= '</ul>';
$html .= '</li>';
}
return $html;
}
- Update your composer.json file with the below.
"autoload": {
"classmap": [
"database"
],
"files" : [
"app/helpers.php"
],
"psr-4": {
"App\\": "app/"
}
},
-
Then type composer dump-autoload -o
-
Here at the controller. Here i assume I already have the tables filled up.
Mytree::rebuild(true); //does not return anything hence when dd will get null
//dd('end');
$Mytree = Mytree::all()->toHierarchy();
return View('testing', ['Mytree' => $Mytree]);
- In the blade template.
<ul>
@foreach($Mytree as $node)
{!!renderNode($node)!!}
@endforeach
</ul>
And you are done:
Although i am surprised that {!!renderNode($node)!!} is equivalent to <?php echo renderNode($node); ?> in terms of blade templating.
And not {{renderNode($node)}} which will display the child together with the HTML as strings...Try and See
Hope this helps.
And my BAUM class declaration:
<?php
namespace App;
use Baum\Node;
/**
* MyTree
*/
class Mytree extends Node {
/**
* Table name.
*
* @var string
*/
protected $table = 'Mytrees';
}