pbergman/tree-helper
Laravel package for working with hierarchical tree data in Eloquent models. A trait and query helpers let you fetch parents, children, ancestors, descendants, and siblings, and build nested trees from flat records for categories, menus, and threaded content.
Minimal steps:
composer require pbergman/tree-helper (the package name and repository name differ — tree-helper on Packagist maps to the pbergman/tree-printer repo).src/), so the fastest way to learn the exact API is to open the main class rather than rely on stale README examples.use Pbergman\TreePrinter\TreePrinter; // verify namespace/method names against src/
$data = [
'app' => [
'config' => ['database.php', 'queue.php'],
'routes' => ['web.php', 'api.php'],
],
'public' => ['index.php', 'assets'],
];
echo (new TreePrinter())->render($data);
If the class exposes static helpers (common for "dump"-style utilities), the call may look like TreePrinter::print($data) — check the source for the actual entry point before wiring it into your code.
Debugging nested arrays and config. The primary value is turning a deep array into something a human can scan in a log or terminal. Wrap it in a tiny dev-only helper so you can drop dumpTree($array) anywhere without leaking it into production output paths.
if (app()->environment('local')) {
logger()->debug((new TreePrinter())->render($payload));
}
Adjacency lists from the database. Most Laravel tree data is stored flat (id, parent_id) — this package renders a nested array, so normalize first. A small recursive builder is usually enough:
function buildTree(Collection $rows, $parentId = null): array
{
$branch = [];
foreach ($rows->where('parent_id', $parentId) as $row) {
$children = buildTree($rows, $row->id);
$branch[$row->name] = $children ?: null;
}
return $branch;
}
echo (new TreePrinter())->render(buildTree(Category::all()));
Custom node formatting. Where the package accepts a callback/closures formatter, use it to attach counts, IDs, or status markers to labels. Keep the formatter pure (return a string) — side effects make the output order-dependent and hard to test.
Artisan command for inspection. A dev-only command is the natural home: php artisan app:tree or php artisan config:tree that prints the tree of a config subtree or a file-system directory. Cache the compiled output so it doesn't rebuild on every keystroke if you add a watch mode.
Directory trees. If you're using it for files, combine with RecursiveDirectoryIterator → nested array, then render. This is the "poor-man's tree command" use case and is a common reason to reach for this package.
Symfony Console / CI logs. The output is just a string, so pipe it through $this->line() or OutputInterface::write(). Consider wrapping in a SymfonyStyle block for a titled section.
├──, └──, and │ render as garbage in some Windows consoles, older CI log viewers, and locales without UTF-8. If output is captured into logs you inspect on multiple platforms, be prepared to post-process to ASCII (+--, `--) or configure an ASCII mode if the package offers one.\n, the tree indentation will look wrong on continuation lines. Pre-collapse newlines to a space or ↳ before returning.require-dev.How can I help you explore Laravel packages today?