Giter VIP home page Giter VIP logo

Comments (8)

lazychaser avatar lazychaser commented on June 15, 2024

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.

darthtaco avatar darthtaco commented on June 15, 2024

@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.

lazychaser avatar lazychaser commented on June 15, 2024

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.

lazychaser avatar lazychaser commented on June 15, 2024

Released v2.3.0 with linkNodes method, no you can do following:

$nodes = TreeNode::defaultOrder()->get()->linkNodes();

foreach ($nodes as $node)

from laravel-nestedset.

darthtaco avatar darthtaco commented on June 15, 2024

@lazychaser that fix works well, and generates only one DB query. Thanks a million!

from laravel-nestedset.

lazychaser avatar lazychaser commented on June 15, 2024

I'm glad that worked!

from laravel-nestedset.

engraniaokasha avatar engraniaokasha commented on June 15, 2024

hi , when i used totree() i did't get parent ..why?

from laravel-nestedset.

ComputerMaverick69 avatar ComputerMaverick69 commented on June 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.