当没有数据

时间:2018-01-31 07:43:55

标签: eloquent laravel-5.5

我使用etrepat baum laravel模块来存储和生成层次结构。还使用eloquent api资源来自定义返回的json数据。

如果没有可用的儿童,则返回当前空的儿童。是否有可能只有孩子可以生孩子?

当前结果

[{
    "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);
}

4 个答案:

答案 0 :(得分:1)

您可以在将其添加到数组之前进行检查,如果它是空的,请不要将其添加到返回的数组中:

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;
}

答案 1 :(得分:1)

在Laravel 5.7中,您可以使用:

public function toArray($request) {
    return [
        'id' => (string) $this->id,
        'text' => (string) $this->name,
        'children' => CategoryResource::collection($this->whenLoaded('children'))
    ];
}

答案 2 :(得分:0)

Laravel 5.7的正确答案

public function toArray($request) {
    return [
        'id' => (string) $this->id,
        'text' => (string) $this->name,
        'children' => CategoryResource::collection($this->when(!$this->children->isEmpty()))
    ];
}

在此示例中,如果子级为空,则将子级键从资源响应中完全删除,然后再发送给客户端。

答案 3 :(得分:0)

如果您还需要此方法才能与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))
    ];
}