Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Treewalker Laravel Package

lukascivil/treewalker

TreeWalker is a lightweight PHP 8.1+ library for manipulating nested arrays, objects, and JSON strings interchangeably. It offers diffing, recursive walking with edits/deletion, merging, and dynamic key-path get/set/create operations, with configurable array, object, or JSON output.

View on GitHub
Deep Wiki
Context7

TreeWalker is a lightweight PHP library for manipulating nested structures, letting you work with arrays, objects, and JSON strings interchangeably. Every method accepts and returns any of these three types, and a configurable returntype setting controls the output format.

  • getdiff() compares two structures and reports new, removed, and edited keys
  • walker() recursively traverses a structure with edit and delete support
  • structMerge() merges structures, with the first argument taking precedence
  • Dynamic key helpers create, read, and set values by key path
  • Requires PHP >= 8.1; install via composer require lukascivil/treewalker
Frequently asked questions about Treewalker
How can I use TreeWalker in a Laravel application to manage nested Eloquent model attributes?
TreeWalker works with arrays, objects, and JSON, so you can convert Eloquent model attributes to an array using `toArray()`, manipulate them with TreeWalker methods like `setDynamicallyValue()`, and then reassign the values. For example, use `$model->setAttribute('key.path', $value)` after transforming the data. Note that strict-typed models may require manual type casting.
Does TreeWalker support Laravel’s API Resources for normalizing nested JSON responses?
Yes, you can use TreeWalker to preprocess nested data before passing it to API Resources. For instance, merge configurations or diff payloads with `getdiff()` to validate or transform data before serialization. The library’s `structMerge()` method is particularly useful for combining API response structures dynamically.
What’s the best way to handle deeply nested data (e.g., 100+ levels) without hitting PHP recursion limits?
TreeWalker uses PHP’s native recursion, so for deeply nested structures, increase `xdebug.max_nesting_level` in your `php.ini` or use iterative approaches like `walker()` with a custom callback to avoid recursion. Test with your expected depth to ensure stability, as PHP’s default limit is often around 100.
Can TreeWalker replace Laravel’s built-in `array_merge_recursive` for merging nested configurations?
Yes, TreeWalker’s `structMerge()` method offers more control, including precedence rules and support for objects/JSON. It’s ideal for merging complex configurations (e.g., user preferences or multi-level API payloads) where `array_merge_recursive` might not handle edge cases like duplicate keys or nested conflicts.
How do I integrate TreeWalker with Laravel’s service container for dependency injection?
Bind TreeWalker to the container in a service provider using `app()->singleton(TreeWalker::class, fn() => new TreeWalker(['returntype' => 'array']))`. Then inject `TreeWalker` into controllers/services via constructor injection. This avoids reinstantiating the class and simplifies configuration.
What are the performance implications of using TreeWalker for large datasets (e.g., 10K+ nested records)?
TreeWalker’s recursive methods may slow down with very large datasets due to PHP’s recursion overhead. For performance-critical cases, benchmark operations like `walker()` or `getdiff()` on sample data. If needed, process data in chunks or use iterative alternatives like `array_walk_recursive` for comparison.
Does TreeWalker work with Laravel’s typed properties (PHP 8.2+) or strict Eloquent models?
TreeWalker dynamically accesses properties, which may conflict with strict-typed models or PHP 8.2’s typed properties. Convert models to arrays (`toArray()`) before manipulation, or use `setAttribute()`/`getAttribute()` for Eloquent models. Avoid direct dynamic property access on strict-typed classes.
Are there Laravel-specific examples or tests for TreeWalker in the official documentation?
The current documentation focuses on generic PHP usage. For Laravel-specific examples, check the live demo or contribute to the project by adding examples for Eloquent integration, API responses, or service container binding. The package is lightweight, so custom examples may be necessary for your use case.
How can I diff two Eloquent model collections to detect changes in nested attributes?
Convert both collections to arrays using `toArray()`, then use `getdiff()` to compare them. The result will show `new`, `removed`, and `edited` keys for nested attributes. For example, `$diff = $treeWalker->getdiff($oldCollection->toArray(), $newCollection->toArray(), true);` for nested output.
What alternatives to TreeWalker exist for Laravel, and when should I choose them?
For Laravel, consider `spatie/array-to-object` for simple array-to-object conversions or Laravel’s built-in `Arrayable`/`Jsonable` traits for basic JSON handling. Use TreeWalker if you need advanced features like diffing, merging, or dynamic key paths. For async processing, pair TreeWalker with Laravel Queues or Symfony Messenger.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
socialdept/tenant-domains
tautvydasr/blade-class-names
socialdept/color-scales
maherelgamil/controller
coleus/widgets
coleus/table
coleus/users
coleus/music
coleus/health
coleus/calendar
coleus/notes
coleus/support
coleus/settings
coleus/apps
seampact/laravel
emmyconceptng/laravel-ng-postcode
jjsquady/round-robin
skyraptor/chatter
edlugz/sasapay
abbasudo/artisan-route