withDataPermission() ->orderBy('id') ->get(); // 2. 组装树结构 $treeData = []; foreach ($hospitals as $hospital) { // 医院节点 $hospitalNode = [ 'id' => $hospital->id, 'title' => $hospital->organ_name, // 医院名称 'children' => [] ]; // 3. 查询该医院下的所有科室 $departments = OpmMwDepartment::where('organ_id', $hospital->id) ->withDataPermission() ->orderBy('sort_id') ->get(); // 4. 组装科室节点 foreach ($departments as $dept) { $hospitalNode['children'][] = [ 'id' => $dept->id, 'title' => $dept->dept_name // 科室名称 ]; } $treeData[] = $hospitalNode; } // 5. 返回 Layui 树需要的格式 return json([ 'code' => 0, 'msg' => 'success', 'data' => $treeData ]); } }