通过关系获取相关数据 [英] Get related data through relation

查看:69
本文介绍了通过关系获取相关数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 5.5.13.

我有App\Entity,其中有许多App\Comment和许多App\Thumb.

现在,我可以像这样轻松获取注释和大拇指:

public function show(Entity $entity)
{
    return $entity->load('comments')->load('thumbs');
}

这给出了这样的数据:

{
    "id": 1,
    "kind": null,
    "name": "three",
    "created_at": "2017-11-01 04:29:22",
    "updated_at": "2017-11-01 04:29:22",
    "comments": [
        {
            "id": 5,
            "body": "no non o",
            "displayname_id": 4,
            "entity_id": 1,
            "created_at": "2017-11-01 05:16:14",
            "updated_at": "2017-11-01 05:16:14"
        }
    ],
    "thumbs": [
        {
            "id": 9,
            "like": 0,
            "displayname_id": 5,
            "entity_id": 1,
            "created_at": "2017-11-01 05:16:39",
            "updated_at": "2017-11-01 05:16:39"
        }
    ]
}

但是,我还需要一个$comment->displaynames()$thumb->displaynames()的列表,并且每个注释通过$comment->votes()都有许多App\Vote.是否有可能.我在Pivot上读了很多书,但我真的很困惑.

我的目标是获取这样的数据:

{
    // removed for concise (same as above)
    "comments": [
        {
            // removed for concise (same as above)
            "votes": [
                ..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
            ]
        },
        {
            // removed for concise (same as above)
            "votes": [
                ..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
            ]
        }
    ],
    "thumbs": [
        {
            // removed for concise (same as above)
        }
    ],
    "displaynames": [
        ..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
    ]
}

我们在这里看到新的displaynames数组和每个comment中的votes数组.

我是否必须在这里使用数据透视来建立一对多关系?

解决方案

您的控制器:

Entity::
with(['comments' => function($query){

    $query->with('votes');

}, 'thumbs' => function($query){

    $query->with('votes');

}])
->find($entity->id);

与关系:

entity has many comments
entity has many thumbs
thumb has many votes
comment has many votes

您应该告诉更多有关displayName关系的信息...

Entity::
with(['comments' => function($query){

    $query->with(['votes', 'displayName']);

}, 'thumbs' => function($query){

    $query->with(['votes', 'displayName']);

}, 'displayName'])
->find($entity->id);

EDIT-2:

Entity::
with(['comments' => function($query){

    $query->with(['votes' => function($query){
       $query->with('displayName');
}, 'displayName']);

}, 'thumbs' => function($query){

    $query->with(['votes' => function($query){
       $query->with('displayName');
}, 'displayName']);

}, 'displayName'])
->find($entity->id);

I am using laravel 5.5.13.

I have App\Entity which has many App\Comment's and many App\Thumb's.

Now I am able to fetch the Comments and Thumbs easily like this:

public function show(Entity $entity)
{
    return $entity->load('comments')->load('thumbs');
}

This gives data like this:

{
    "id": 1,
    "kind": null,
    "name": "three",
    "created_at": "2017-11-01 04:29:22",
    "updated_at": "2017-11-01 04:29:22",
    "comments": [
        {
            "id": 5,
            "body": "no non o",
            "displayname_id": 4,
            "entity_id": 1,
            "created_at": "2017-11-01 05:16:14",
            "updated_at": "2017-11-01 05:16:14"
        }
    ],
    "thumbs": [
        {
            "id": 9,
            "like": 0,
            "displayname_id": 5,
            "entity_id": 1,
            "created_at": "2017-11-01 05:16:39",
            "updated_at": "2017-11-01 05:16:39"
        }
    ]
}

However, I also need a list of the $comment->displaynames() and $thumb->displaynames() and also each comment has many App\Votes via the $comment->votes(). Is it possible. I read lots on pivot but I am real confused.

My goal is to get data like this:

{
    // removed for concise (same as above)
    "comments": [
        {
            // removed for concise (same as above)
            "votes": [
                ..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
            ]
        },
        {
            // removed for concise (same as above)
            "votes": [
                ..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
            ]
        }
    ],
    "thumbs": [
        {
            // removed for concise (same as above)
        }
    ],
    "displaynames": [
        ..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
    ]
}

We see here the new displaynames array and a votes array in each comment.

Do i have to use pivot here to in a one-to-many relationship?

解决方案

Your controller:

Entity::
with(['comments' => function($query){

    $query->with('votes');

}, 'thumbs' => function($query){

    $query->with('votes');

}])
->find($entity->id);

And relations:

entity has many comments
entity has many thumbs
thumb has many votes
comment has many votes

And you should tell more specific information about displayName relation...

EDIT:

Entity::
with(['comments' => function($query){

    $query->with(['votes', 'displayName']);

}, 'thumbs' => function($query){

    $query->with(['votes', 'displayName']);

}, 'displayName'])
->find($entity->id);

EDIT-2:

Entity::
with(['comments' => function($query){

    $query->with(['votes' => function($query){
       $query->with('displayName');
}, 'displayName']);

}, 'thumbs' => function($query){

    $query->with(['votes' => function($query){
       $query->with('displayName');
}, 'displayName']);

}, 'displayName'])
->find($entity->id);

这篇关于通过关系获取相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆