Closed GregOstry closed 2 years ago
Hi @GregOstry, What do you mean by "but this gives me an extra relationship"? What do you do with the tree? Convert it to JSON? What should the tree look like?
Hi,
For each parent and child element I have a morp "collectable" relation. So instead of attributes from the origin table (collection) I would like to see the relation directly.
Is it possible to call the relation "collectable" recrusively?
The End Effect in UI should look like this: One Article has 4 or more attributes (In German: Bezeichgnung2, Grammatur, Material, Langtext). In this attributes there are several sections (like Allgemein, ash, sport grey, etc.). And finally, each section contains a text element.
I hope I have written this understandably. :)
If I understand you correctly, you can add a recursive method like this to your controller (?):
protected function collectableTree($tree) {
$result = collect();
foreach ($tree as $collection) {
$collection->collectable->setRelation('children', $this->collectableTree($collection->children));
$result[] = $collection->collectable;
}
return $result;
}
$tree = Collection::with('collectable')
->treeOf(function($query) {
$query->whereNull('parent_id')->where('id', 1);
})
->orderBy('order')
->get()
->toTree();
//->first();
dump($this->collectableTree($tree));
dump($this->collectableTree($tree)->first());
First of all i want to thank you for this awesome package. This helps me a lot to implement it in my use case.
I have a collections table that hold such polymorphic data:
Collection Model:
Text\Attribute\Article Model:
Is it possible to display Collection table (see above) with the Polymorphic data in the tree structure?
One solution is to eager load the relations, but this gives me an extra relationsship.
Maybe there is a more elegant solution for this?