【问题标题】:Get related data through relation通过relation获取相关数据
【发布时间】:2018-04-13 09:55:08
【问题描述】:

我使用的是 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() 的列表,并且每个评论都有许多App\Votes 通过$comment->votes()。可能吗。我读了很多关于枢轴的文章,但我真的很困惑。

我的目标是得到这样的数据:

{
    // 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 数组。

我是否必须在此处使用枢轴来建立一对多关系?

【问题讨论】:

    标签: laravel-5.5


    【解决方案1】:

    你的控制器:

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

    【讨论】:

    • 立即尝试!谢谢你,先生!我的显示名称关系是 displayname has many thumbsdisplayname has many votesdisplayname has many comments
    • 哇,太酷了!有用!然而一件小事,评论有投票,投票有一个显示名称。因此,对于每次投票,我都需要显示显示名称。目前是:'comments' =&gt; function($q) { $q-&gt;with('votes')-&gt;with('displayname'); }with('displayname') 这里给了我评论的显示名。我还需要每次投票的显示名称。 :(
    • 非常感谢先生的编辑!!!如果您请添加一些 cmets,我也很乐意对此表示赞同。但是$query-&gt;with(['votes', 'displayName']); 不起作用。它仍然给我comment 中的displayName。我需要每张选票的 displayName :(
    • 没关系 :) 逻辑很简单。它是递归的复制和粘贴。
    • 是的,在 Laravel 中可以使用 Collections,但您的数据是输出。我应该试试
    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多