Comments (8)
Hi! If you're using my package, it can be done easily. First of all, you need to query all nodes and convert them to a tree like so:
$tree = TreeNode::all()->toTree();
This will fill parent
and children
relations for each node. Now we can use recursion for building path:
public function stringPath()
{
$parent = $this->parent;
return $parent ? $parent->stringPath().' > '.$this->Name : $this->Name;
}
This will be much faster, but we can optimize this further by caching path:
protected $stringPath;
public function stringPath()
{
if ($this->stringPath) return $this->stringPath;
$parent = $this->parent;
return $this->stringPath = $parent ? $parent->stringPath().' > '.$this->Name : $this->Name;
}
from laravel-nestedset.
@lazychaser thanks so much for responding so quickly.
That method does not appear to work for me. When I run a foreach
loop over the results from $tree = TreeNode::all()->toTree()
, only the three root nodes are returned, and so I never get any deeper than the first level. So, when calling the stringPath()
method, I only get the path to the root nodes.
I tried using your stringPath()
method above with the results from TreeNode::all()
(without calling toTree()
) and, while I got the correct output, it generated over 1,400 queries to the database.
I am using L5 and your version 2.2.0.
Any suggestions?
from laravel-nestedset.
Yeah, you right, this is my mistake, you need to call toTree
on different collection to fill relations and then iterate over original collection:
$nodes = TreeNode::all();
\Kalnoy\Nestedset\Collection::make($nodes)->toTree();
// iterate over $nodes
foreach ($nodes as $node)
In fact, I believe I should extract relationships filling to a separate method on collection.
from laravel-nestedset.
Released v2.3.0 with linkNodes
method, no you can do following:
$nodes = TreeNode::defaultOrder()->get()->linkNodes();
foreach ($nodes as $node)
from laravel-nestedset.
@lazychaser that fix works well, and generates only one DB query. Thanks a million!
from laravel-nestedset.
I'm glad that worked!
from laravel-nestedset.
hi , when i used totree() i did't get parent ..why?
from laravel-nestedset.
Released v2.3.0 with
linkNodes
method, no you can do following:$nodes = TreeNode::defaultOrder()->get()->linkNodes(); foreach ($nodes as $node)
This seemed to work in getting the relationships however the children collection returns an empty array despite there being a child node to a parent node.
from laravel-nestedset.
Related Issues (20)
- Integrity constraint violation error when deleting a node. HOT 1
- How to Move Between Root
- Use fully qualified key on QueryBuilder
- Problem with deadlock and broken tree when performing new registrations.
- My workaround to rebuildTree().
- My node has no parent, but has ancestors HOT 3
- Laravel 10 support HOT 4
- How to restrict descendants by status? HOT 1
- Efficiently restrict number of levels when using descendants()
- Is it possible to move QueryBuilder logic to trait? To support custom Builders
- You May Ordered Children with adding this method.
- Code runs in SQL error for height = 0 in QueryBuilder::columnPatch(...)
- fix parent function name
- Binary Tree Function
- finding siblings and parents
- Parent _lft and _rgt not updated after adding child using `create()` method HOT 2
- Sibling of the same category will be move as per id or record list. HOT 1
- Undefined method `append`
- Laravel 11 HOT 1
- Problem to require in Laravel 11 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-nestedset.