如何在 Laravel blade 文件中的 nestedset 自定义函数中传递控制器数据值
How to pass Controller data value in nestedset custom function in Laravel blade file
在下面找到控制器代码和blade文件代码,
已测试并正在工作
控制器代码:
$nodes = Category::get()->toTree();
Blade 文件代码
Returning Value = 6 {{$data->category_id}}
我需要在下面的 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.'"> '.$category->name.'</label> <br> </li>';
}
$traverse($category->children, $prefix.'-');
}
};
$traverse($nodes);
@endphp
无需任何修改,此遍历功能运行良好。
所以当我需要传递一个额外的值来与另一个控制器进行比较时,如何处理它?
为此场景使用 lazychaser/laravel-nestedset
包。
使用
解决
use (&$traverse,&$data_cat_id)
修改为
@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.'"> '.$category->name.'</label> <br> </li>';
}
$traverse($category->children, $prefix.'-');
}
};
$traverse($nodes);
@endphp
在下面找到控制器代码和blade文件代码,
已测试并正在工作
控制器代码:
$nodes = Category::get()->toTree();
Blade 文件代码
Returning Value = 6 {{$data->category_id}}
我需要在下面的 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.'"> '.$category->name.'</label> <br> </li>';
}
$traverse($category->children, $prefix.'-');
}
};
$traverse($nodes);
@endphp
无需任何修改,此遍历功能运行良好。
所以当我需要传递一个额外的值来与另一个控制器进行比较时,如何处理它?
为此场景使用 lazychaser/laravel-nestedset
包。
使用
解决use (&$traverse,&$data_cat_id)
修改为
@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.'"> '.$category->name.'</label> <br> </li>';
}
$traverse($category->children, $prefix.'-');
}
};
$traverse($nodes);
@endphp