How we can find the level of a node in a tree using kalnoy/nestedset package | Laravel | nestedset

935 Views Asked by At

I am working on a Laravel application where I used kalnoy/nestedset package to make the tree structure for color categories. Now I want to know the level of the node related to the tree.

For example

Colors
    Red
        Dark red
            very dark red
        Light red
    Green
        Dark Green
    Blue
        Dark Blue

In my above context, I have the main three colors. So the main colors have level 1 and children of those have level 2 and so on.

Now the problem is how can we get the level of a node from the node instance

$color = Color::find(12); // let says it is "very dark red color"

echo "Get level of the above color";

Why

Because I actually allow the user to add custom colors if not present in the system. But I don't want to allow the end-user to create color for level 4. So a user can create up to 3 levels of colors.

So in my above case if someone sends me the "Very dark red" color instance as a parent whose level is 3 then further no more children should be created from this parent.

0

There are 0 best solutions below