-
Anonymous
Сгладить вложенные отношения laravel (от родителя к потомкам)
Сообщение
Anonymous »
В моей модели есть отношения родитель-потомок. Я хочу сгладить всю эту коллекцию, включая всех потомков родителя.
Код: Выделить всё
[
{
'name': 'Parent',
'another_array' => [
{
'name': 'Another Array Name'
}
],
'children': [
{
'name': 'First Child',
'address': 'lorem ipsum',
'contact_no': '92390',
'another_array' => [
{
'name': 'Another Array Name'
}
],
'children': [
{
'name': 'First Descendant',
'address': 'lorem ipsum',
'contact_no': '92390',
'children': [
{
'name': 'Descendant Child',
'address': 'lorem ipsum',
'contact_no': '92390',
'children': [
{
'name': 'Last Descendant',
'address': '',
'contact_no': '',
'children': []
}
]
}
]
}
]
}
]
}
]
Я попробовал код ниже. Однако он отображает только вторую глубину детей. Следующие дочерние элементы коллекции не отображаются.
Код: Выделить всё
User.php
public function parent()
{
return $this->belongsTo(User::class, 'id', 'parent_id');
}
public function children()
{
return $this->hasMany(User::class,'parent_id', 'id');
}
public function descendants()
{
return $this->children()->with('descendants');
}
UsersController.php
public function index()
{
$parent = User::find(1);
$parent->with('descendants')->find($parent->id);
$descendants = $this->traverseTree($parent->descendants);
return response($descendants);
}
protected function traverseTree($subtree)
{
$descendants = collect([]);
foreach ($subtree->descendants as $descendant) {
$descendants->push($descendant);
if($descendant->descendants instanceof Collection) {
foreach ($descendant->descendants as $node) {
$this->traverseTree($node);
$descendants->push($node);
}
}
}
return $descendants;
}
Желаемый результат
Код: Выделить всё
[
{
'name': 'Parent',
'address': 'lorem ipsum',
'contact_no': '92390'
},
{
'name': 'First Child',
'address': 'lorem ipsum',
'contact_no': '92390'
},
{
'name': 'First Descendant',
'address': 'lorem ipsum',
'contact_no': '92390',
},
{
'name': 'Descendant Child',
'address': 'lorem ipsum',
'contact_no': '92390',
},
{
'name': 'Last Descendant' ,
'address': '',
'contact_no': '',
}
]
Жду вашей помощи.
Подробнее здесь:
https://stackoverflow.com/questions/570 ... escendants
1720252800
Anonymous
В моей модели есть отношения родитель-потомок. Я хочу сгладить всю эту коллекцию, включая всех потомков родителя.
[code][
{
'name': 'Parent',
'another_array' => [
{
'name': 'Another Array Name'
}
],
'children': [
{
'name': 'First Child',
'address': 'lorem ipsum',
'contact_no': '92390',
'another_array' => [
{
'name': 'Another Array Name'
}
],
'children': [
{
'name': 'First Descendant',
'address': 'lorem ipsum',
'contact_no': '92390',
'children': [
{
'name': 'Descendant Child',
'address': 'lorem ipsum',
'contact_no': '92390',
'children': [
{
'name': 'Last Descendant',
'address': '',
'contact_no': '',
'children': []
}
]
}
]
}
]
}
]
}
]
[/code]
Я попробовал код ниже. Однако он отображает только вторую глубину детей. Следующие дочерние элементы коллекции не отображаются.
[code]User.php
public function parent()
{
return $this->belongsTo(User::class, 'id', 'parent_id');
}
public function children()
{
return $this->hasMany(User::class,'parent_id', 'id');
}
public function descendants()
{
return $this->children()->with('descendants');
}
UsersController.php
public function index()
{
$parent = User::find(1);
$parent->with('descendants')->find($parent->id);
$descendants = $this->traverseTree($parent->descendants);
return response($descendants);
}
protected function traverseTree($subtree)
{
$descendants = collect([]);
foreach ($subtree->descendants as $descendant) {
$descendants->push($descendant);
if($descendant->descendants instanceof Collection) {
foreach ($descendant->descendants as $node) {
$this->traverseTree($node);
$descendants->push($node);
}
}
}
return $descendants;
}
[/code]
Желаемый результат
[code]
[
{
'name': 'Parent',
'address': 'lorem ipsum',
'contact_no': '92390'
},
{
'name': 'First Child',
'address': 'lorem ipsum',
'contact_no': '92390'
},
{
'name': 'First Descendant',
'address': 'lorem ipsum',
'contact_no': '92390',
},
{
'name': 'Descendant Child',
'address': 'lorem ipsum',
'contact_no': '92390',
},
{
'name': 'Last Descendant' ,
'address': '',
'contact_no': '',
}
]
[/code]
Жду вашей помощи.
Подробнее здесь: [url]https://stackoverflow.com/questions/57039677/flatten-laravel-nested-relationship-parent-to-descendants[/url]