Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

_camilo_'s avatar

Trouble retrieving JSON data from API

Hi there! It's my first time using Laravel as an API, and I can't understand this odd behavior: for some reason I get all my JSON structure with the keys, but all the values are null.

See, this is my API response:

[
  {
    "title": null,
    "url": null,
    "children": [
      {
        "title": null,
        "url": null
      }
    ]
  },
  {
    "title": null,
    "url": null
  }
]

And when i dd() the data before it gets send, everything is normal:

array:2 [▼
  0 => array:3 [▼
    "title" => "Menu item 1"
    "url" => "/item-1"
    "children" => array:1 [▼
      0 => array:2 [▼
        "title" => "Menu item 2"
        "url" => "/item-3"
      ]
    ]
  ]
  1 => array:2 [▼
    "title" => "Menu item 3"
    "url" => "/item-2"
  ]
]

This is the response statement, which sends the above structure:

return response()->json($data, 200);

Appreciate the help!

0 likes
6 replies
_camilo_'s avatar

@Sinnbeck Hi omnipresent being!

I'm getting the data with kalnoy/nestedset methods plus a custom method that converts the nested collection to an array.

$menus = $this->model->defaultOrder()
    ->get()
    ->toTree();

$data = $this->model->treeToArray($menus);

# in App\Models\Menu

public function treeToArray(Collection $tree)
{
    if (!$tree->count()) {
        return null;
    } else {
        return array_map(function ($item) {
            $data = array();
            $data['title'] = $item->title;
            $data['url'] = $item->url;
            if ($item->descendants()->count() > 0) {
                $data['children'] = self::treeToArray($item->descendants()->withdepth()->get());
            }
            return $data;
        }, $tree->all());
    }
}
_camilo_'s avatar

Must say that when I pass a handcrafted array such as the below, it works fine. So probably is something with this treeToArray() method...

[
    [
        "title" => "Menu item 1",
        "url" => "/item-1",
        "children" => [
            [
                "title" => "Menu item 2",
                "url" => "/item-3",
            ]
        ]
    ],
    [
        "title" => "Menu item 3",
        "url" => "/item-2",
    ]
]
_camilo_'s avatar

So I've tracked deeper and find the cause, but not the solution.

I'm using the "astrotomic/laravel-translatable": "^11.9.1" package, and the translated attributes are the ones which are becoming null in the response()->json() (but are ok in the dd()).

_camilo_'s avatar
_camilo_
OP
Best Answer
Level 1

Got it. To end my monolog, I let the adaptation of the treeToArray() function that made it work.

public function treeToArray(Collection $tree)
{
    if (!$tree->count()) {
        return null;
    } else {
        return array_map(function ($item) {
            $trans = $item->translations->whereIn('locale', 'pt-BR')->first();
            $data = array();
            $data['title'] = $trans->title;
            $data['url'] = $trans->url;
            if ($item->descendants()->count() > 0) {
                $data['children'] = self::treeToArray($item->descendants()->withdepth()->get());
            }
            return $data;
        }, $tree->all());
    }
}

Please or to participate in this conversation.