I have nested todos. Every todo hasMany todo. I try to get all todos on the same level with staudenmeir/laravel-adjacency-list
I have a collection of todos.
[
[{
"id": 30000000,
"todo_number": 1,
"order": 1,
"company_id": 1,
"todoable_id": 20000000,
"todoable_type": "issue",
"title": "1. Todo",
"children_and_self": [{
"id": 30000000,
"todo_number": 1,
"order": 1,
"company_id": 1,
"todoable_id": 20000000,
"todoable_type": "issue",
"title": "1. Todo"
}, {
"id": 30000001,
"todo_number": 1,
"order": 1,
"company_id": 1,
"todoable_id": 30000000,
"todoable_type": "todo",
"title": "2. Todo"
}]
}, {
"id": 30000003,
"todo_number": 2,
"order": 2,
"company_id": 1,
"todoable_id": 20000000,
"todoable_type": "issue",
"title": "4. Todo",
"closer": null,
"todos": [],
"children_and_self": [{
"id": 30000003,
"todo_number": 2,
"order": 2,
"company_id": 1,
"todoable_id": 20000000,
"todoable_type": "issue",
"title": "4. Todo"
}]
}]
]
return $allTodos[0][0]; shows the following result:
{
"id": 30000000,
"todo_number": 1,
"order": 1,
"company_id": 1,
"todoable_id": 20000000,
"todoable_type": "issue",
"title": "1. Todo",
"children_and_self": [
{
"id": 30000000,
"todo_number": 1,
"order": 1,
"company_id": 1,
"todoable_id": 20000000,
"todoable_type": "issue",
"title": "1. Todo",
]
},
{
"id": 30000001,
"todo_number": 1,
"order": 1,
"company_id": 1,
"todoable_id": 30000000,
"todoable_type": "todo",
"title": "2. Todo",
}
]
}
return $allTodos[0][0]->id; shows 30000000 as expected. Till here everything works well.
But if I try to return $allTodos[0][0]->children_and_self; it shows nothing.
If I try to return var_dump($allTodos[0][0]->children_and_self); it shows NULL
Why children_and_self are NULL? What I’m missing?
Advertisement
Answer
JSON response provides snake case results, but Laravel relations are written as camel case.
So as I mentioned in the comment you will need something like this:
var_dump($allTodos[0][0]->childrenAndSelf)