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

      <legend id='LVk1A'><style id='LVk1A'><dir id='LVk1A'><q id='LVk1A'></q></dir></style></legend>
      • <bdo id='LVk1A'></bdo><ul id='LVk1A'></ul>
      <tfoot id='LVk1A'></tfoot>

        Laravel 合并关系

        时间:2023-09-24

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

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

                • <small id='FKsOU'></small><noframes id='FKsOU'>

                    <tbody id='FKsOU'></tbody>
                  本文介绍了Laravel 合并关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有没有办法在 Laravel 中合并 2 个关系?

                  Is there a way to merge 2 relationships in laravel?

                  这是它现在的设置方式,但是有没有办法可以将两者合并?

                  this is the way it's setup now, but Is there a way I could return both merged?

                    public function CompetitionsHome() {
                      return $this->HasMany( 'Competition', 'home_team_id' );
                    }
                    public function CompetitionsGuest() {
                      return $this->HasMany( 'Competition', 'guest_team_id' );
                    }
                    public function Competitions() {
                      // return both CompetitionsHome & CompetitionsGuest
                    }
                  

                  推荐答案

                  尝试返回从关系返回的合并集合的属性的 getter 方法.

                  Try out getter method for property which returns merged collections returned from relations.

                  public function getCompetitionsAttribute($value)
                  {
                      // There two calls return collections
                      // as defined in relations.
                      $competitionsHome = $this->competitionsHome;
                      $competitionsGuest = $this->competitionsGuest;
                  
                      // Merge collections and return single collection.
                      return $competitionsHome->merge($competitionsGuest);
                  }
                  

                  或者您可以在返回集合之前调用其他方法以获得不同的结果集.

                  Or you can call additional methods before collection is returned to get different result sets.

                  public function getCompetitionsAttribute($value)
                  {
                      // There two calls return collections
                      // as defined in relations.
                      // `latest()` method is shorthand for `orderBy('created_at', 'desc')`
                      // method call.
                      $competitionsHome = $this->competitionsHome()->latest()->get();
                      $competitionsGuest = $this->competitionsGuest()->latest()->get();
                  
                      // Merge collections and return single collection.
                      return $competitionsHome->merge($competitionsGuest);
                  }
                  

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

                  上一篇:Laravel Eloquent:如何对相关模型的结果进行排序? 下一篇:如何在使用 eloquent 时排除某些列

                  相关文章

                  最新文章

                • <tfoot id='2ABMU'></tfoot>
                • <legend id='2ABMU'><style id='2ABMU'><dir id='2ABMU'><q id='2ABMU'></q></dir></style></legend>
                  1. <small id='2ABMU'></small><noframes id='2ABMU'>

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

                        <bdo id='2ABMU'></bdo><ul id='2ABMU'></ul>