title: Cannot use object of type stdClass as array using laravel
body: <p><a href="https://i.stack.imgur.com/HUNiS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HUNiS.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/ouTbI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ouTbI.png" alt="enter image description here"></a>I have made function to display all my privileges against the menu<br>
  My Controller code below:</p>
  
  <pre><code>&lt;?php
      class PrivilegesController extends Controller
      {
          public function getAllPrivileges()
          {
              $privileges  = DB::table('prev_definition')-&gt;orderBy('id', 'asc')-&gt;get();
              $privileges  = $this-&gt;build_heirarchy($privileges);
  
              $resultArray = ['status' =&gt; true, 'message' =&gt; 'Privileges found!', 'data' =&gt; $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-&gt;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;
          }
      }
  ?&gt;
  </code></pre>
  
  <p>But I'm getting error of Cannot use object of type <code>stdClass</code> as array I'm so confused how I can make it happen and working
  your help will be highly appreciated!</p>
  
  <pre><code>{
    "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')-&gt;orderBy('id', 'asc')-&gt;get();
                 $privileges = $this-&gt;build_heirarchy($privileges);
  
      $resultArray = ['status' =&gt; true, 'message' =&gt; 'Privileges found!', 'data' =&gt; $privileges];
  
      return json_encode($resultArray);
  
  }
  
  function build_heirarchy($result_set, $parent_id = 0)
  {
      $rs = array();
      foreach ($result_set as $row) {
          $row-&gt;is_checked = 1; // here is error
          if ($row-&gt;parent_id == $parent_id) {
              $children = $this-&gt;build_heirarchy($result_set, $row-&gt;id);
              if ($children) {
                  if ($children[0]-&gt;type == 'menu') {
                      $type = 'submenu';
                  } else {
                      $type = 'permission';
                  }
                  $row-&gt;{$type} = $children; // keys to object can only set using curly braces if variable
              }
              $rs[] = $row;
          }
      }
      return $rs;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>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</p>