2016-07-30 120 views
0
工作

我里面收集藏品看起来像:上laravel嵌套集合

=> [ 
[ 
"id" => 3, 
    "parent_id" => 2, 
    "depth" => 1, 
    "children" => [ 
    [ 
     "id" => 4, 
     "parent_id" => 3, 
     "depth" => 2, 
     "children" => [ 
     [ 
      "id" => 5, 
      "parent_id" => 4, 
      "depth" => 3, 
      "children" => [ 
      [ 
    [...] 

我怎么能对这项工作? $ result->计数();

这回1

它表现得像只有1集,所以我甚至无法使用地图或每进行过滤。我需要过滤最大深度为4.

我试过拒绝(),但它也不工作。

我可以把它转换为数组,但我想用最好的收集方法...

回答

1

你需要通过你的藏品

$result->children->map(function ($item) { 
    // do some stuff with $item or you can no map through its children 
    $item->children->map(function ($nestedItem) { 
     // do some stuff with $nestedItem etc// 
    }); 
}); 

迭代的替代方法是使用虽然声明foreach

+0

This return me:Null – TheShun

+0

在此处发布您的代码,而不仅仅是变量的转储 –

+0

我在我的User模型上使用此https://github.com/franzose/ClosureTable。 为了得到这个转储我只是做: $ result = $ user-> getDescendantsTree(); 我不能在这里发布所有类的闭包表,但结果是嵌套的集合。 – TheShun

0

第一集合转换成数组,然后使用

count($result, COUNT_RECURSIVE)