0

enter image description hereenter image description hereI have made function to display all my privileges against the menu
My Controller code below:

<?php
    class PrivilegesController extends Controller
    {
        public function getAllPrivileges()
        {
            $privileges  = DB::table('prev_definition')->orderBy('id', 'asc')->get();
            $privileges  = $this->build_heirarchy($privileges);

            $resultArray = ['status' => true, 'message' => 'Privileges found!', 'data' => $privileges];
            return json_encode($resultArray);
        }

        public function build_heirarchy($result_set, $parent_id = 0)
        {
            $rs = array();
            foreach ($result_set as $row) {

                $row['is_checked'] = 1; // here is error
                if ($row['parent_id'] == $parent_id) {

                    $children = $this->build_heirarchy($result_set, $row['id']);
                    if ($children) {

                        if ($children[0]['type'] == 'menu') {

                            $type = 'submenu';
                        } else {
                            $type = 'permission';
                        }
                        $row[$type] = $children;
                    }
                    $rs[] = $row;
                }
            }
            return $rs;
        }
    }
?>

But I'm getting error of Cannot use object of type stdClass as array I'm so confused how I can make it happen and working your help will be highly appreciated!

{
  "data": [
    {
      "created_at": "2019-05-20 15:48:34",
      "deletedAt": null,
      "display_group": "patient",
      "icon": "NULL",
      "id": 297,
      "isDeleted": null,
      "is_checked": 1,
      "parent_id": 0,
      "priv_key": "can_access_patient",
      "priv_title": "Patient",
      "type": "menu",
      "updated_at": "2019-05-20 15:48:34"
    }
  ],
  "message": "Privileges found!",
  "status": true
}


                   class PrivilegesController extends Controller
        {
                  public function getAllPrivileges()
{
              $privileges = DB::table('prev_definition')->orderBy('id', 'asc')->get();
               $privileges = $this->build_heirarchy($privileges);

    $resultArray = ['status' => true, 'message' => 'Privileges found!', 'data' => $privileges];

    return json_encode($resultArray);

}

function build_heirarchy($result_set, $parent_id = 0)
{
    $rs = array();
    foreach ($result_set as $row) {
        $row->is_checked = 1; // here is error
        if ($row->parent_id == $parent_id) {
            $children = $this->build_heirarchy($result_set, $row->id);
            if ($children) {
                if ($children[0]->type == 'menu') {
                    $type = 'submenu';
                } else {
                    $type = 'permission';
                }
                $row->{$type} = $children; // keys to object can only set using curly braces if variable
            }
            $rs[] = $row;
        }
    }
    return $rs;
}

}

This is my final controller code and i have also shared the response with you can look into this and can let me know if there is any more changges

Rahul
  • 18,271
  • 7
  • 41
  • 60
syed1234
  • 761
  • 5
  • 12
  • 30

2 Answers2

1

As per your suggestions and comments I modified your code, once check and let me know.

class PrivilegesController extends Controller
{
    public function getAllPrivileges()
    {
        $privileges = DB::table('prev_definition')->orderBy('id', 'asc')->get();
        // add below line, keep your old code as it is and check
        $privileges  = json_decode(json_encode($privileges), true);
        $privileges  = $this->build_heirarchy($privileges);
        $resultArray = ['status' => true, 'message' => 'Privileges found!', 'data' => $privileges];
        return json_encode($resultArray);
    }
    public function build_heirarchy($result_set, $parent_id = 0)
    {
        $rs = array();
        foreach ($result_set as $row) {
            $row['is_checked'] = 1; // here is error
            if ($row['parent_id'] == $parent_id) {
                $children = $this->build_heirarchy($result_set, $row['id']);
                if ($children) {
                    if ($children[0]['type'] == 'menu') {
                        $type = 'submenu';
                    } else {
                        $type = 'permission';
                    }
                    $row[$type] = $children;
                }
                $rs[] = $row;
            }
        }
        return $rs;
    }
}

EDIT

  1. There is only record with parent id 0, so it will create only one parent record, and recursively it will have appended child data. Check this.
  2. Now for parent id 1 it has 2 records in screenshot, so two elements will appended inside parent single most element.
  3. Now for parent id 2 it has 4 records in screenshot, so 4 elements will be appended inside element with id 2.
  4. This will go on inside to inside of every element considering parent id.
  5. Subsequently all data will be appended to every parent of it. So index is only one and data is appended recursively to its every relevant parent.

I hope I am clear.

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/193661/discussion-on-answer-by-quickswap-cannot-use-object-of-type-stdclass-as-array-us). – Samuel Liew May 20 '19 at 23:51
0
if ($children)
{
    if($children[0]->type=='menu')
        $type = 'submenu';
    else
        $type = 'permission';

    $row[$type] = $children;
}
Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
iAmGroot
  • 950
  • 1
  • 6
  • 22