How do I flatten laravel recursive relationship collection (tree collections)?
<p>How do I flatten a collection with hierarchy self referenced models, tree collections into a single dimension collection. I have a self referencing model having parents and children.</p>

<p><strong>I want the result to return a eloquent collection, not a simple collection or an array. array has been used as result results for easy demonstration</strong></p>

<p>relationships are declared like this.</p>

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

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

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

public function childrenRecursive()
{
    return $this-&gt;children()-&gt;with('childrenRecursive');
}
</code></pre>

<p>so when i call the <code>model-&gt;childrenRecursive</code> it returns the collection as it should be. like this. i have changed it <code>toArray()</code> to make it easy to read.</p>

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

<p>what I want to achieve is the collection to be single dimension. here is how the <code>toArray()</code> to that collection should look like.</p>

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

<p>I have tried many collection methods like <code>filter</code>, <code>flatMap</code>, <code>flatten</code> and multiple array methods. but haven't found an appropriate solution.</p>