laravel 5.4层次结构/树使用查询构建器

时间:2017-05-29 17:29:58

标签: php laravel tree

我正在尝试创建一个公司组织树,但我无法让事情发挥作用。

我希望你们中的一些聪明人会对我的错误有所启发。

我的数据库只有几行,看起来像是测试......

  • id,parent_id,client_id,department_id
  • 1,0,1,4
  • 2,1,1,4
  • 3,2,1,4
  • 4,0,1,4

我所能接触到的只是这个

array:1
["children" => array:2
    [0 => array:2
        [
            "id" => 0
            "name" => "Marketing Director"
        ]
     1 => array:2
        [
            "id" => 0
            "name" => "Accounts Director"
        ]
    ]
]

但我真正想要的是父子结构

父母>小孩>孩子等等...

到目前为止,这是我的代码......

public static function generateOrgTree($clientid, $parent)
{
    $all_cats = DB::table('client_roles_rel')
           ->join('client_job_roles', 'client_job_roles.role_id', '=', 'client_roles_rel.role_id')
           ->join('client_departments', 'client_departments.department_id', '=', 'client_roles_rel.department_id')
           ->where('client_roles_rel.client_id', '=', $clientid)
           ->orderby('client_roles_rel.rel_id', 'asc')
           ->get();

    $tree = [];

    foreach ($all_cats as $cats) 
    {
        $pid = $cats->parent_id;
        $id = $cats->id;
        $department = $cats->department_name;

        if($pid == '0')
        {
            $tree['children'][] = ['id' => $pid, 'name' => $rolename];
        }
        else
        {
            if($pid == $parent) 
            {
                $children = General::generateOrgTree($clientid, $id);

                $pid = $cats->parent_id;
                $id = $cats->id;
                $department = $cats->department_name;

                $tree['children'][] = ['id' => $pid, 'name' => $rolename];
            }
        }
    }

    //dd(json_encode($tree));
    return $tree;
}

2 个答案:

答案 0 :(得分:0)

好的,所以没有回复,但我得到了这个工作,这是我的代码供任何人使用,如果你有更好的方法这样做,那么请分享。

public static function generateOrgTree($elements, $parent, $num)
{
        $tree = [];

        //Log::info('Num: ' . $num);

        if($num <= 10)
        {

            foreach ($elements as $element) 
            {
                $pid = $element->parent_id;
                $id   = $element->rel_id;
                $relid = $element->rel_id;
                $rolename = $element->job_roles;
                $department = $element->department_name;

                $element->title = $rolename;
                $element->name = $department;

                if($pid == $parent)
                {
                    $children = General::generateOrgTree($elements, $id, $num +1);

                    if ($children) {

                        //$element['children'] = $children;
                        //$children->department_name = $children->department_name;

                        $element->children = $children;
                    }

                    $tree[] = $element;


                }

            }

        }

        return $tree;
}

如何使用:使用(数组,父ID,限制)调用函数并将集合传递给我的视图:

$getOrg = General::generateOrgTree($all_cats, 0, 0);

$tree = collect($getOrg);

->with('orgtree', $tree->toJson())

在我看来,我正在使用OrgChart https://github.com/dabeng/OrgChart

<script type="text/javascript">

$(document).ready(function(){

 var dataset = jQuery.parseJSON('<?php echo $orgtree; ?>');

  dataset.forEach(function(e) {

   // rename or remove properties from the data set...
   //e.title = e.department_name;
   delete e.department_name; 
   //e.name = e.job_roles;

   delete e.job_roles; 
   delete e.parent_id; 
   delete e.rel_id; 


});

  // console.log(dataset);

  var datasource = {'name': 'Company', 'title': '{{$clientdetails->client_company_name}}', 'children': dataset};

  $('#chart-container').orgchart({
    'data' : datasource,
    'nodeContent': 'title',
    'exportButton': false,
    'exportFilename': 'MyOrgChart',
    'draggable': false,
  });


});

</script>

答案 1 :(得分:0)

大概是这样的:

$staff = Personnel::all()->keyBy('id');
$bosses = [];
foreach($staff as $member)
{
  if($member->parent_id)
  {
      $parent = $staff[$member->parent_id];
      if (!is_array($parent->children)) $parent->children = [];
      $parent->children[] = $member;
  }
  else
  {
      $bosses[] = $member;
  }
}

之后,$bosses数组中的层次结构顶级honchos数组以及children属性中每个节点的子节点数组。如果您只需要构建树,则无需递归。