Get related data through relation

让人想犯罪 __ 提交于 2019-12-13 00:35:17

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/47048804/get-related-data-through-relation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!