如何在 Laravel 5.7 中返回集合的子集合?我有一个 Laravel 集合,它本身包含一个集合。当我通过我的 api 返回父集合时,显然有一个“子”对象,但是当我尝试返回“子”对象时它为空。
相关收藏代码:
$item = [
'id' => $this->id,
...
$this->mergeWhen($this->type === Item::[type], [
...
'children' => [type]::collection($this->[prop])
]),
...
return $item;
];
相关api控制器代码:
$itemsQuery = $[parent_type]->items()->topLevel()->withAll()->ordered()->get();
$items = [type]::collection($itemsQuery);
$return_array = [];
foreach ($items as $item)
{
array_push($return_array, $item);
}
return $return_array;
这将返回我所期望的。
"data": {
"id": [guid],
...
"children": [
{
"id": [guid],
...
}
]
}
但是当我把它改成
$itemsQuery = $[parent_type]->items()->topLevel()->withAll()->ordered()->get();
$items = [type]::collection($itemsQuery);
$return_array = [];
foreach ($items as $item)
{
array_push($return_array, $item->children);
}
return $return_array;
我明白了
"data": null
红糖糍粑
慕后森
随时随地看视频慕课网APP