Saravanan
Saravanan

Reputation: 1237

Laravel Eloquent API resource is returning empty array when no data

I am using etrepat baum laravel module to store and generate hierarchy. Also using eloquent api resource to customize the json data returned.

Currently empty children are returned if no children available. Is it possible to have children only if available?

Current Result

[{
    "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": []
    }]
}]

Expected Result

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

Upvotes: 3

Views: 5172

Answers (4)

Edward Millen
Edward Millen

Reputation: 224

If you also need this to work correctly with Conditional Relationships (i.e. not trigger loading of each relationship just to check whether it's empty), this is the best way I could figure out (based partly on the other answers here, plus some trial and error)

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

Upvotes: 0

llioor
llioor

Reputation: 6238

Correct answer for Laravel 5.7

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

In this example, if the children is empty, the children key will be removed from the resource response entirely before it is sent to the client.

Upvotes: 0

Saranga A
Saranga A

Reputation: 1091

In Laravel 5.7, you could use:

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

Upvotes: 1

Maraboc
Maraboc

Reputation: 11083

You can check before adding it to the array and if it's empty don't add it to the returned array :

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

Upvotes: 3

Related Questions