<tfoot id='WMyxh'></tfoot>

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

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

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

      1. <legend id='WMyxh'><style id='WMyxh'><dir id='WMyxh'><q id='WMyxh'></q></dir></style></legend>
      2. L5.6 - 数据透视表的关系

        时间:2023-09-22

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

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

                  本文介绍了L5.6 - 数据透视表的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在枢轴表上有一个关系;我如何扩展它?

                  I've a relation on a pivot table; how I can expand It?

                  例如:

                  商店:

                  • id
                  • 姓名

                  产品:

                  • id
                  • 姓名

                  product_shop:

                  • product_id
                  • shop_id
                  • field_1
                  • field_2
                  • field_3
                  • table_A_id

                  table_A:

                  • id
                  • 姓名

                  Shops 模型中的多对多关系是:

                  The relation Many-to-Many in the Shops Model is:

                  class Shops extends Model {
                      public function products()
                      {
                          return $this->belongsToMany('Products', 'product_shop', 'product_id', 'shop_id')->withPivot(
                              'field_1',
                              'field_3',
                              'field_3',
                              'table_A_id'
                              )
                              ->as('product_shop')
                              ->withTimestamps();
                      }
                  

                  }

                  检索所有数据的查询是:

                  and the query to retrieve all data is:

                  class GetData extends Model {
                       public static function getAll() {
                           $query = Shops::with(
                              'products'
                              )->get();
                       }
                  }
                  

                  这会返回product_shop.table_A_id,但我想扩展外键并检索table_A.name;有办法吗?

                  This return the product_shop.table_A_id but I'd like to expand the foreign key and retrieve table_A.name; is there a way?

                  谢谢.

                  推荐答案

                  您可以使用枢轴模型:

                  class ProductShopPivot extends IlluminateDatabaseEloquentRelationsPivot
                  {
                      public function tableA()
                      {
                          return $this->belongsTo(TableA::class);
                      }
                  }
                  
                  class Shops extends Model
                  {
                      public function products()
                      {
                          return $this->belongsToMany('Products', 'product_shop', 'product_id', 'shop_id')
                              ->withPivot(
                                  'field_1',
                                  'field_3',
                                  'field_3',
                                  'table_A_id'
                              )
                              ->as('product_shop')
                              ->withTimestamps()
                              ->using(ProductShopPivot::class);
                      }
                  }
                  

                  然后像这样访问它:

                  $shop->product_shop->tableA->name
                  

                  不幸的是,没有办法预先加载 tableA 关系.

                  Unfortunately, there is no way to eager load the tableA relation.

                  这篇关于L5.6 - 数据透视表的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何从laravel eloquent unique中删除索引号 下一篇:如何使 Laravel 的 Validator $rules 成为可选?

                  相关文章

                  最新文章

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

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

                        <bdo id='u5oGG'></bdo><ul id='u5oGG'></ul>