Laravel Eloquent API 资源在没有数据时返回空数组
Laravel Eloquent API resource is returning empty array when no data
我正在使用 etrepat baum laravel 模块来存储和生成层次结构。还使用 eloquent api 资源自定义返回的 json 数据。
如果没有 children 可用,则返回当前为空 children。是否有可能只有 children 可用?
当前结果
[{
"id": "1",
"text": "Vegetables",
"children": [{
"id": "2",
"text": "Onion",
"children": []
}, {
"id": "3",
"text": "Tomato",
"children": []
}, {
"id": "4",
"text": "Chilli",
"children": []
}, {
"id": "5",
"text": "Potato",
"children": []
}]
}, {
"id": "6",
"text": "Fruits",
"children": [{
"id": "7",
"text": "Apple",
"children": [{
"id": "12",
"text": "Red Apple",
"children": []
}, {
"id": "13",
"text": "Green Apple",
"children": []
}]
}, {
"id": "8",
"text": "Banana",
"children": [{
"id": "14",
"text": "Red Banana",
"children": []
}, {
"id": "15",
"text": "Yellow Banana",
"children": []
}]
}, {
"id": "9",
"text": "Orange",
"children": []
}, {
"id": "10",
"text": "Papaya",
"children": []
}, {
"id": "11",
"text": "Guava",
"children": []
}]
}]
预期结果
[{
"id": "1",
"text": "Vegetables",
"children": [{
"id": "2",
"text": "Onion"
}, {
"id": "3",
"text": "Tomato"
}, {
"id": "4",
"text": "Chilli"
}, {
"id": "5",
"text": "Potato"
}]
}, {
"id": "6",
"text": "Fruits",
"children": [{
"id": "7",
"text": "Apple",
"children": [{
"id": "12",
"text": "Red Apple"
}, {
"id": "13",
"text": "Green Apple"
}]
}, {
"id": "8",
"text": "Banana",
"children": [{
"id": "14",
"text": "Red Banana"
}, {
"id": "15",
"text": "Yellow Banana"
}]
}, {
"id": "9",
"text": "Orange"
}, {
"id": "10",
"text": "Papaya"
}, {
"id": "11",
"text": "Guava"
}]
}]
CategoryResource.php
public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->children)
];
}
CategoryController.php
public function index()
{
CategoryResource::withoutWrapping();
$categories = Category::all()->toHierarchy()->values();
return new CategoryResourceCollection($categories);
}
您可以在将其添加到数组之前进行检查,如果它为空,则不要将其添加到返回的数组中:
public function toArray($request) {
$result = [
'id' => (string) $this->id,
'text' => (string) $this->name
];
$child = CategoryResource::collection($this->children);
if (!$child->isEmpty()) {
$result['children'] = $child;
}
return $result;
}
在 Laravel 5.7 中,您可以使用:
public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->whenLoaded('children'))
];
}
Laravel 5.7
的正确答案
public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->when(!$this->children->isEmpty()))
];
}
在这个例子中,如果 children 为空,children 键将在发送到客户端之前从资源响应中完全删除。
如果您还需要它与 Conditional Relationships 一起正常工作(即不触发每个关系的加载只是为了检查它是否为空),这是我能找到的最好方法(部分基于另一个在这里回答,加上一些试验和错误)
public function toArray($request)
{
$children = $this->whenLoaded('children');
return [
'id' => $this->id,
'text' => $this->text,
'children' => $this->when(method_exists($children, 'isEmpty') && !$children->isEmpty(), CategoryResource::collection($children))
];
}
我正在使用 etrepat baum laravel 模块来存储和生成层次结构。还使用 eloquent api 资源自定义返回的 json 数据。
如果没有 children 可用,则返回当前为空 children。是否有可能只有 children 可用?
当前结果
[{
"id": "1",
"text": "Vegetables",
"children": [{
"id": "2",
"text": "Onion",
"children": []
}, {
"id": "3",
"text": "Tomato",
"children": []
}, {
"id": "4",
"text": "Chilli",
"children": []
}, {
"id": "5",
"text": "Potato",
"children": []
}]
}, {
"id": "6",
"text": "Fruits",
"children": [{
"id": "7",
"text": "Apple",
"children": [{
"id": "12",
"text": "Red Apple",
"children": []
}, {
"id": "13",
"text": "Green Apple",
"children": []
}]
}, {
"id": "8",
"text": "Banana",
"children": [{
"id": "14",
"text": "Red Banana",
"children": []
}, {
"id": "15",
"text": "Yellow Banana",
"children": []
}]
}, {
"id": "9",
"text": "Orange",
"children": []
}, {
"id": "10",
"text": "Papaya",
"children": []
}, {
"id": "11",
"text": "Guava",
"children": []
}]
}]
预期结果
[{
"id": "1",
"text": "Vegetables",
"children": [{
"id": "2",
"text": "Onion"
}, {
"id": "3",
"text": "Tomato"
}, {
"id": "4",
"text": "Chilli"
}, {
"id": "5",
"text": "Potato"
}]
}, {
"id": "6",
"text": "Fruits",
"children": [{
"id": "7",
"text": "Apple",
"children": [{
"id": "12",
"text": "Red Apple"
}, {
"id": "13",
"text": "Green Apple"
}]
}, {
"id": "8",
"text": "Banana",
"children": [{
"id": "14",
"text": "Red Banana"
}, {
"id": "15",
"text": "Yellow Banana"
}]
}, {
"id": "9",
"text": "Orange"
}, {
"id": "10",
"text": "Papaya"
}, {
"id": "11",
"text": "Guava"
}]
}]
CategoryResource.php
public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->children)
];
}
CategoryController.php
public function index()
{
CategoryResource::withoutWrapping();
$categories = Category::all()->toHierarchy()->values();
return new CategoryResourceCollection($categories);
}
您可以在将其添加到数组之前进行检查,如果它为空,则不要将其添加到返回的数组中:
public function toArray($request) {
$result = [
'id' => (string) $this->id,
'text' => (string) $this->name
];
$child = CategoryResource::collection($this->children);
if (!$child->isEmpty()) {
$result['children'] = $child;
}
return $result;
}
在 Laravel 5.7 中,您可以使用:
public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->whenLoaded('children'))
];
}
Laravel 5.7
的正确答案public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->when(!$this->children->isEmpty()))
];
}
在这个例子中,如果 children 为空,children 键将在发送到客户端之前从资源响应中完全删除。
如果您还需要它与 Conditional Relationships 一起正常工作(即不触发每个关系的加载只是为了检查它是否为空),这是我能找到的最好方法(部分基于另一个在这里回答,加上一些试验和错误)
public function toArray($request)
{
$children = $this->whenLoaded('children');
return [
'id' => $this->id,
'text' => $this->text,
'children' => $this->when(method_exists($children, 'isEmpty') && !$children->isEmpty(), CategoryResource::collection($children))
];
}