【问题标题】:what's wrong with my foreach in laravel?我在 laravel 中的 foreach 有什么问题?
【发布时间】:2018-01-27 06:50:19
【问题描述】:

我将在数组中传递数据 但这是代码

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'));
}

我在第一轮的 foreach 运行良好 但最终响应 [ dd($menu) ] 空数组和错误 500 ! 我的英语不好抱歉;)

【问题讨论】:

  • 这是因为你的递归函数没有基本情况来打破递归。
  • mishe bishtar tovzih bedin jenab moghadam ;) ??
  • 你的 $row_for_menu = Menu::where('parent_id', '=', $pid)->get();是空的
  • :|不为空,第一轮foreach效果不错!

标签: php laravel foreach controller


【解决方案1】:

试试这个演示是否正常

 public  function  test($pid){
    $row_for_menu = Menu::where('parent_id', '=', $pid)->get()->lists('id','name');
    $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);
 }

【讨论】:

    【解决方案2】:

    $menu 是空的,即。 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】:

      您应该在模型上定义菜单项之间的关系,而不是递归循环来构建菜单。

      // 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'));
      }
      

      【讨论】:

        【解决方案4】:

        测试函数必须像这样在 foreach 之后返回 $menu

        public  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)
        

        【讨论】:

          猜你喜欢
          • 2018-08-19
          • 2016-04-26
          • 2017-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多