无法使用 laravel 将 stdClass 类型的对象用作数组

问题描述 投票:0回答:2

我已经实现了在菜单上显示我所有权限的功能。

我的控制器代码如下:

<?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;
        }
    }
?>

但我收到此错误:

无法将

stdClass
类型的对象用作数组

我很困惑如何才能实现它并工作。

{
  "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;
}
}

这是我的最终控制器代码,我也分享了响应。我需要做出哪些改变?

php arrays laravel multidimensional-array
2个回答
1
投票

根据您的建议和评论,我修改了您的代码,检查后请告诉我。

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

编辑

  1. 只有父 id 为 0 的记录,因此它只会创建一个父记录,并且递归地会附加子数据。检查这个
  2. 现在对于父 id 1,它在屏幕截图中有 2 条记录,因此两个元素将附加到父单个最元素内。
  3. 现在对于父 id 2,它在屏幕截图中有 4 条记录,因此 4 个元素将被附加到 id 2 的元素内。
  4. 这将在考虑父元素的每个元素的内部进行 id。
  5. 随后所有数据将被附加到它的每个父级。因此索引只有一个,数据会递归地附加到其每个相关的父级。

我希望我说清楚了。


0
投票
if ($children)
{
    if($children[0]->type=='menu')
        $type = 'submenu';
    else
        $type = 'permission';

    $row[$type] = $children;
}
© www.soinside.com 2019 - 2024. All rights reserved.