亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 中的遞歸菜單

Laravel 中的遞歸菜單

PHP
慕容708150 2021-06-30 14:19:40
我正在通過以下方式構建遞歸菜單:public static function tree(){    return static::with(implode('.', array_fill(0, 100, 'children')))->where('parent_id', '=', '0')        ->join('company_components', 'id', '=', 'company_components.component_id')        ->where('company_components.company_id', '=', auth('api')->user()->with('company')->first()->company_id)        ->orderBy('id')        ->get();}我得到這樣的東西:[  {    "id":1,    "parent_id":0,    "name":"Cuentas",    "url":"",    "icon":"fa fa-user",    "deleted_at":null,    "created_at":"2019-05-26 18:57:02",    "updated_at":"2019-05-26 18:57:02",    "company_id":1,    "component_id":1,    "children":[        {          "id":2,          "parent_id":1,          "name":"Crear Cuenta",          "url":"/account/create",          "icon":"fa fa-circle-o",          "deleted_at":null,          "created_at":null,          "updated_at":null,          "children":[]        }     ]  }]如何避免最后一個元素的空數組(不再有子元素)?如果“孩子”沒有更多孩子,有沒有辦法不創建元素?
查看完整描述

1 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

  $this->cleanMenu($menu);


    public function cleanMenu($tree)

    {

        foreach($tree as $t) {

            if (!$t->children->count() > 0) {

                unset($t['children']);

            } else {

                $this->cleanMenu($t->children);

            }

        }

    }

如果有一個解決方案從模型中求解而無需再次迭代,那將是非常了不起的!


我希望有所幫助


查看完整回答
反對 回復 2021-07-09
  • 1 回答
  • 0 關注
  • 281 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號