<i id='oXokW'><tr id='oXokW'><dt id='oXokW'><q id='oXokW'><span id='oXokW'><b id='oXokW'><form id='oXokW'><ins id='oXokW'></ins><ul id='oXokW'></ul><sub id='oXokW'></sub></form><legend id='oXokW'></legend><bdo id='oXokW'><pre id='oXokW'><center id='oXokW'></center></pre></bdo></b><th id='oXokW'></th></span></q></dt></tr></i><div id='oXokW'><tfoot id='oXokW'></tfoot><dl id='oXokW'><fieldset id='oXokW'></fieldset></dl></div>
      <bdo id='oXokW'></bdo><ul id='oXokW'></ul>

    <legend id='oXokW'><style id='oXokW'><dir id='oXokW'><q id='oXokW'></q></dir></style></legend>

    <tfoot id='oXokW'></tfoot>

    <small id='oXokW'></small><noframes id='oXokW'>

      如何合并两个 Eloquent 集合?

      时间:2023-09-24

      <small id='GWG1O'></small><noframes id='GWG1O'>

        1. <i id='GWG1O'><tr id='GWG1O'><dt id='GWG1O'><q id='GWG1O'><span id='GWG1O'><b id='GWG1O'><form id='GWG1O'><ins id='GWG1O'></ins><ul id='GWG1O'></ul><sub id='GWG1O'></sub></form><legend id='GWG1O'></legend><bdo id='GWG1O'><pre id='GWG1O'><center id='GWG1O'></center></pre></bdo></b><th id='GWG1O'></th></span></q></dt></tr></i><div id='GWG1O'><tfoot id='GWG1O'></tfoot><dl id='GWG1O'><fieldset id='GWG1O'></fieldset></dl></div>
              <bdo id='GWG1O'></bdo><ul id='GWG1O'></ul>
                <tbody id='GWG1O'></tbody>
              <legend id='GWG1O'><style id='GWG1O'><dir id='GWG1O'><q id='GWG1O'></q></dir></style></legend>

              <tfoot id='GWG1O'></tfoot>

                本文介绍了如何合并两个 Eloquent 集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个问题表和一个标签表.我想从给定问题的标签中获取所有问题.因此,例如,我可能将标签旅行"、火车"和文化"附加到给定的问题.我希望能够获取这三个标签的所有问题.看起来棘手的是,问题和标签的关系在 Eloquent 中是多对多的,定义为belongsToMany.

                I have a questions table and a tags table. I want to fetch all questions from tags of a given question. So, for example, I may have the tags "Travel," "Trains" and "Culture" attached to a given question. I want to be able to fetch all questions for those three tags. The tricky, so it seems, is that questions and tags relationship is a many-to-many defined in Eloquent as belongsToMany.

                我想尝试合并问题集合如下:

                I thought about trying to merge the questions Collections as below:

                foreach ($question->tags as $tag) {
                    if (!isset($related)) {
                        $related = $tag->questions;
                    } else {
                        $related->merge($tag->questions);
                    }
                }
                

                虽然它似乎不起作用.似乎没有合并任何东西.我是否正确地尝试了这个?另外,是否有更好的方法在 Eloquent 中以多对多关系获取一行行?

                It doesn't seem to work though. Doesn't seem to merge anything. Am I attempting this correctly? Also, is there perhaps a better way to fetch a row of rows in a many-to-many relationship in Eloquent?

                推荐答案

                merge 方法返回的是合并后的集合,不会对原集合进行变异,因此需要做如下操作

                The merge method returns the merged collection, it doesn't mutate the original collection, thus you need to do the following

                $original = new Collection(['foo']);
                
                $latest = new Collection(['bar']);
                
                $merged = $original->merge($latest); // Contains foo and bar.
                

                将示例应用到您的代码中

                Applying the example to your code

                $related = new Collection();
                
                foreach ($question->tags as $tag)
                {
                    $related = $related->merge($tag->questions);
                }
                

                这篇关于如何合并两个 Eloquent 集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel Eloquent 按关系表列排序 下一篇:Eloquent 模型大规模更新

                相关文章

                最新文章

                <i id='fp7cD'><tr id='fp7cD'><dt id='fp7cD'><q id='fp7cD'><span id='fp7cD'><b id='fp7cD'><form id='fp7cD'><ins id='fp7cD'></ins><ul id='fp7cD'></ul><sub id='fp7cD'></sub></form><legend id='fp7cD'></legend><bdo id='fp7cD'><pre id='fp7cD'><center id='fp7cD'></center></pre></bdo></b><th id='fp7cD'></th></span></q></dt></tr></i><div id='fp7cD'><tfoot id='fp7cD'></tfoot><dl id='fp7cD'><fieldset id='fp7cD'></fieldset></dl></div>
                1. <tfoot id='fp7cD'></tfoot>

                2. <small id='fp7cD'></small><noframes id='fp7cD'>

                  <legend id='fp7cD'><style id='fp7cD'><dir id='fp7cD'><q id='fp7cD'></q></dir></style></legend>

                  • <bdo id='fp7cD'></bdo><ul id='fp7cD'></ul>