简体   繁体   English

为什么 Laravel 不返回集合中的项目

[英]Why Laravel does not return an item from a collection

I have nested todos .我嵌套了todos Every todo hasMany todo.每一个todo hasMany待办事项。 I try to get all todos on the same level with staudenmeir/laravel-adjacency-list我尝试使用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.按预期显示30000000 Till here everything works well.到这里一切正常。

But if I try to return $allTodos[0][0]->children_and_self;但是如果我尝试return $allTodos[0][0]->children_and_self; it shows nothing.它什么也没显示。

If I try to return var_dump($allTodos[0][0]->children_and_self);如果我尝试return var_dump($allTodos[0][0]->children_and_self); it shows NULL它显示NULL

Why children_and_self are NULL?为什么children_and_self是NULL? What I'm missing?我缺少什么?

JSON response provides snake case results, but Laravel relations are written as camel case. JSON 响应提供了蛇形大小写的结果,但 Laravel 关系被写成驼色大小写。

So as I mentioned in the comment you will need something like this:所以正如我在评论中提到的,你需要这样的东西:

var_dump($allTodos[0][0]->childrenAndSelf)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM