jeudi 17 mai 2018

Laravel 5.5 recursive function to create menu

I have created a mysql table as below:

id | menuname          | parentid
---+-------------------+---------
 1 | dashboard         |        0
 2 | Content           |        0
 3 | Home Page Content |        2
 4 | Banners           |        2
 5 | Settings          |        0
 6 | Block Content     |        3
 7 | Site Content      |        3

So that the menu structure will look like:

  • dashboard
  • Content
    • Home Page Content
      • Block Content
      • Site Content
  • Banners
  • Settings

I have the controller:

    public function index() {
    $data = array();
    $permissionRecord = Permission::all();
    $this->categoryTree($permissionRecord);
    dd('-end-);
    $data['permissionRecord'] = $permissionRecord;
    return view('Administrator.permission.permissionAdd',$data);
    }

function categoryTree($permissionRecord, $parent_id = 0, $sub_mark = ''){
            foreach($permissionRecord as $row){
            //while($row = $query->fetch_assoc()){
                echo $sub_mark.$row->name;
                $this->categoryTree($permissionRecord, $row->id, $sub_mark.'---');
            }

    }

But this show the data :

Dashboard---Dashboard------Dashboard---------Dashboard------------Dashboard---------------Dashboard------------------Dashboard---------------------Dashboard------------------------Dashboard---------------------------Dashboard------------------------------Dashboard---------------------------------Dashboard------------------------------------Dashboard

Please note, I dd() inside the controller and did not passed the data to the view.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire