phplaravellaravel-bladenested-sets

How to pass Controller data value in nestedset custom function in Laravel blade file


Find below the controller code and blade file code,

Tested & Working

Controller Code:

$nodes = Category::get()->toTree();

Blade File Code

Returning Value = 6 {{$data->category_id}}

I need to pass this variable value inside the below traverse function of NestedSet Laravel Package

    @php
    
    $traverse = function ($categories, $prefix = '-') use (&$traverse) {
        foreach ($categories as $category) {
            if($data->category_id==$category->id) { //How to return Category id Here to Compare ($data->category_id Not returning any value here)
                echo $prefix.' <li class="branch"><label><input type="radio" checked name="parent_id"
                value="'.$category->id.'">&nbsp;'.$category->name.'</label> <br> </li>';
            }
        $traverse($category->children, $prefix.'-');
        }
    };
    
    $traverse($nodes);
    
    @endphp

Without any modification, this traverse function working well.

So How to deal with it, when I need to pass an additional value to compare from another controller?

Using lazychaser/laravel-nestedset Package for this scenario.


Solution

  • Solved Using

    use (&$traverse,&$data_cat_id)
    

    Modified as

    @php
    $data_cat_id = $data->category_id;
        
        $traverse = function ($categories, $prefix = '-') use (&$traverse,&$data_cat_id) {
            foreach ($categories as $category) {
                if($data_cat_id==$category->id) { //How to return Category id Here to Compare ($data->category_id Not returning any value here)
                    echo $prefix.' <li class="branch"><label><input type="radio" checked name="parent_id"
                    value="'.$category->id.'">&nbsp;'.$category->name.'</label> <br> </li>';
                }
            $traverse($category->children, $prefix.'-');
            }
        };
        
        $traverse($nodes);
        
        @endphp