我要在数组中传递数据 但这是代码
public function test($pid){
$row_for_menu = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($row_for_menu as $menu_one_by_one){
$menu[] = array('title' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
dd($menu);
}
public function index(Request $request)
{
$this->test(1);
//$perPage = 25;
//$menu = Menu::paginate($perPage);
//return view('admin.menu.index', compact('menu'));
}
我在第一轮比赛中的表现很好
但最终响应[dd($ menu)]空数组和错误500!
我的英语不是很抱歉;)
答案 0 :(得分:0)
试试这个演示工作正常
{{1}}
答案 1 :(得分:0)
您应该在模型上定义菜单项之间的关系,而不是递归循环来构建菜单。
// Menu model
public function parent ()
{
return $this->belongsTo(__CLASS__, 'parent_id');
}
public function children ()
{
return $this->hasMany(__CLASS__);
}
然后在你看来:
@foreach ($menu->children as $child)
// $child->name
// $child->link
@endoreach
您的控制器只是:
// assuming you pass a menu id for specific page or similar
public function index(Request $request) {
$menu = Menu::find($request->getAttribute('menu_id'))->toArray();
return view('admin.menu.index', compact('menu'));
}
答案 2 :(得分:0)
$ menu是空的ie。 array(),因为$ row_for_menu为空
public function test($pid){
$row_for_menu = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($row_for_menu as $menu_one_by_one){
$menu[] = array('title' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
if(count($menu)){
dd($menu);
}
}
public function index(Request $request)
{
$this->test(1);
//$perPage = 25;
//$menu = Menu::paginate($perPage);
//return view('admin.menu.index', compact('menu'));
}
答案 3 :(得分:0)
测试函数必须在像这样的
之后返回$ menupublic function test($pid){
$menus = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($menus as $menu_one_by_one){
$menu[] = array('text' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
return $menu;
}
public function index(Request $request)
{
$x = $this->test(1);
dd($menu)