lukascivil/treewalker
Lightweight PHP library to traverse and manipulate nested data (arrays, objects, JSON) interchangeably. Includes recursive walker, diffing (new/removed/edited), structure merging, and dynamic get/set helpers. Supports PHP 8.1+.
lukascivil/treewalker package excels at traversing, manipulating, and transforming tree-like structures (e.g., nested arrays, category hierarchies, org charts, or file systems). This aligns well with Laravel applications requiring recursive data operations (e.g., nested resource relationships, multi-level menus, or hierarchical permissions).NodeCreated/NodeUpdated events).hasMany/belongsTo trees (e.g., Category->posts with nested comments).TreeWalker::lazy() for generators or implement depth limits.TreeBuilder) to generate test data.hasMany/belongsTo relationships for ORM-backed trees.Collection methods alongside TreeWalker for hybrid operations.<ul> menus) with minimal logic.parent_id columns).getNestedCategories()) with TreeWalker.// Before
function getNestedCategories(Category $category) {
return $category->children->map(fn($child) => [
'name' => $child->name,
'children' => getNestedCategories($child),
]);
}
// After
$tree = TreeWalker::walk($categories, function ($node) {
return [
'name' => $node->name,
'children' => $node->children,
];
});
parent_id is nullable and indexed.path column (e.g., 1/4/7 for node 7 under 4 under 1).spatie/laravel-medialibrary, laravel/breeze).TreeWalker with Laravel-specific adapters (e.g., EloquentTreeWalker).with('children') in Eloquent).NodeCreated triggers tree rebuilds).TreeWalker config).lazy() for large trees).dd(TreeWalker::getState()).WITH RECURSIVE in PostgreSQL).How can I help you explore Laravel packages today?