简体   繁体   English

laravel 获取帖子列表,带有标签和评论

[英]laravel get a list of posts, with their tags, and comments

The answer to this is below.答案如下。

I am trying to get a list of say 10 posts, and add each posts tags and comments (which will vary depending on the post) I have an example of what I am trying to achieve below.我正在尝试获取一个包含 10 个帖子的列表,并添加每个帖子的标签和评论(这将因帖子而异)我在下面有一个我想要实现的示例。

Essentially I am following this as a start.基本上,我将以此作为开始。 https://laravel.com/docs/5.8/eloquent-relationships#many-to-many https://laravel.com/docs/5.8/eloquent-relationships#many-to-many

However its not quite working.Its set up verbatim and I have the pivot tables set up as described.然而,它不太工作。它逐字设置,我按照描述设置了数据透视表。 Either I can get all posts, but by 1 tag, or 1 post with all its tags.要么我可以获得所有帖子,但通过 1 个标签,或 1 个带有所有标签的帖子。 ie IE

"post_title": "Post 1 ",
    "tags": [
            {
                "tag_name": "Tag 1"
            },{
                "tag_name": "Tag 2"
            },... 

I have also tried to use the join method but end up with this situation.我也尝试过使用 join 方法,但最终遇到了这种情况。

 [{ 
    "post_title": "Post 1 ", <--- post 1
    "tag_name": "Tag 1"
  },
  {
    "post_title": "Post 1 ", <--- Also post 1
    "tag_name": "Tag 2"
  },
  {
    "post_title": "Post 3 ",
    "tag_name": "Tag 2"
  },
  {
    "post_title": "Post 4 ",
    "tag_name": "Tag 1"
  }]

As you can see the first 2 have duplicate post names.如您所见,前 2 个帖子名称重复。 I should have post 1 with both tags added.我应该在帖子 1 中添加两个标签。

The way I seem to be able to achieve this is to join everything, then modify, and group elements or even worse get the latest 10 posts, then extract the post ids into an array.我似乎能够实现这一点的方法是加入所有内容,然后修改和分组元素,甚至更糟的是获取最新的 10 个帖子,然后将帖子 ID 提取到一个数组中。 Then with those post ids, get all the categories where the post ids match, Then repeat for comments.然后使用这些帖子 ID,获取帖子 ID 匹配的所有类别,然后重复评论。 Then zip this together.然后把它拉在一起。 This seems super clunky.这看起来超级笨重。

It's odd as this seems like it should be everywhere in google.这很奇怪,因为这似乎应该在 google 中随处可见。 If it is, sorry.如果是,对不起。

There has to be a simple solution for Laravel for me to get the structure below. Laravel 必须有一个简单的解决方案才能让我得到下面的结构。 The question is, is it better to use Eloquent or the join query from Database section to solve this problem?问题是,使用 Eloquent 还是使用 Database 部分的 join 查询来解决这个问题更好?

[{ 
    "post_title": "Post 1 ", 
    "cat": [
        {
          'catname': "Tag 1"
        },
        {
          'catname': "Tag 2"
        },
    ],
    "comment": [
        {
            'comment': "Here be a comment"
        },
        {
            'comment': "Here is one two"
        },
    ],
},
{ 
    "post_title": "Post 2 ", 
    "cat": [
        {
        'catname': "Tag 1"
        }
    ],
    "comment": [],
}
...
]

Answer To solve this problem use Eager loading https://laravel.com/docs/5.8/eloquent-relationships#eager-loading回答要解决此问题,请使用Eager loading https://laravel.com/docs/5.8/eloquent-relationships#eager-loading

Thank you Tim Lewis & Alec Joy!谢谢蒂姆·刘易斯和亚历克·乔伊!

To solve this problem, use Eager loading要解决此问题,请使用Eager loading

(posted as community wiki-answer because this question has been answered by the asker) (作为社区 wiki-answer 发布,因为此问题已由提问者回答)

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

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