I ran into this problem before. The problem is that if we return Eloquent objects we cannot attach non-column attributes to it.

In the case of the tree_builder() it will try to attach children to each object.

    $item->children = '<ul>'.tree_builder($item->children, $html).'</ul>';

But children is not a column and so it chokes.

That is why, in this case, I had kept this part as a Query Builder because it returns regular objects, allowing us to set the children property.