我已经实现了在菜单上显示我所有权限的功能。
我的控制器代码如下:
<?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;
}
}
这是我的最终控制器代码,我也分享了响应。我需要做出哪些改变?
根据您的建议和评论,我修改了您的代码,检查后请告诉我。
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;
}
}
编辑
我希望我说清楚了。
if ($children)
{
if($children[0]->type=='menu')
$type = 'submenu';
else
$type = 'permission';
$row[$type] = $children;
}