2016-12-26 104 views
0

如何將具有層次結構自引用模型的樹集合平鋪到單個維集合中。我有一個父母和孩子的自我參照模型。我如何使laravel遞歸關係集合(樹集合)變平?

我想要結果返回一個雄辯的集合,而不是一個簡單的集合或數組。數組已被用作結果結果以便簡單說明

關係是這樣聲明的。

public function parent() 
{ 
    return $this->belongsTo(self::class, 'parent_id'); 
} 

public function parentRecursive() 
{ 
    return $this->parent()->with('parentRecursive'); 
} 

public function children() 
{ 
    return $this->hasMany(self::class, 'parent_id'); 
} 

public function childrenRecursive() 
{ 
    return $this->children()->with('childrenRecursive'); 
} 

因此,當我打電話model->childrenRecursive它返回所應當的集合。喜歡這個。我已將其更改爲toArray()以便於閱讀。

array:1 [ 
    0 => array:6 [ 
    "id" => 5 
    "name" => "I am a child of 1" 
    "parent_id" => "1" 
    "created_at" => "2016-12-26 13:53:50" 
    "updated_at" => "2016-12-26 13:53:50" 
    "children_recursive" => array:1 [ 
     0 => array:6 [ 
     "id" => 6 
     "name" => "I am child of 5" 
     "parent_id" => "5" 
     "created_at" => "2016-12-26 13:53:50" 
     "updated_at" => "2016-12-26 13:53:50" 
     "children_recursive" => array:2 [ 
      0 => array:6 [ 
      "id" => 7 
      "name" => "I am child of 6" 
      "parent_id" => "6" 
      "created_at" => "2016-12-26 13:53:50" 
      "updated_at" => "2016-12-26 13:53:50" 
      "children_recursive" => [] 
      ], 
      1 => array:6 [ 
      "id" => 8 
      "name" => "I am child of 6 too" 
      "parent_id" => "6" 
      "created_at" => "2016-12-26 13:53:50" 
      "updated_at" => "2016-12-26 13:53:50" 
      "children_recursive" => [] 
      ] 
     ] 
     ] 
    ] 
    ] 
] 

我想要實現的是集合是單一維度。這裏是該集合的toArray()應該是什麼樣子。

array:4 [ 
    0 => array:6 [ 
    "id" => 5 
    "name" => "I am a child of 1" 
    "parent_id" => "1" 
    "created_at" => "2016-12-26 13:53:50" 
    "updated_at" => "2016-12-26 13:53:50" 
    ], 
    1 => array:6 [ 
    "id" => 6 
    "name" => "I am child of 5" 
    "parent_id" => "5" 
    "created_at" => "2016-12-26 13:53:50" 
    "updated_at" => "2016-12-26 13:53:50" 
    ], 
    2 => array:6 [ 
    "id" => 7 
    "name" => "I am child of 6" 
    "parent_id" => "6" 
    "created_at" => "2016-12-26 13:53:50" 
    "updated_at" => "2016-12-26 13:53:50" 
    ], 
    3 => array:6 [ 
    "id" => 8 
    "name" => "I am child of 6 too" 
    "parent_id" => "6" 
    "created_at" => "2016-12-26 13:53:50" 
    "updated_at" => "2016-12-26 13:53:50" 
    ] 
] 

我已經嘗試許多收集方法等filterflatMapflatten和多個陣列的方法。但還沒有找到合適的解決方案。 謝謝

+0

試圖崩潰? –

+0

返回一個空集合 – aimme

回答

2

我沒有發現任何內建方法到Laravel集合中。你可以嘗試這樣的事情(使用它作爲一個全局函數或作爲專用類方法,它給你這裏的想法):

function flatten($array) { 
    $result = []; 
    foreach ($array as $item) { 
     if (is_array($item)) { 
      $result[] = array_filter($item, function($array) { 
       return ! is_array($array); 
      }); 
      $result = array_merge($result, flatten($item)); 
     } 
    } 
    return array_filter($result); 
} 

然後使用它是這樣的:

// When available into global scope as a function 
$flattenArray = flatten($arrayFromTheCollection); 
+1

謝謝。目前我正在做這樣的事情:) – aimme

+1

不客氣:-) –