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

    • <bdo id='uhwSK'></bdo><ul id='uhwSK'></ul>
  • <tfoot id='uhwSK'></tfoot>

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

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

        laravel 中的急切加载关系,并带有关系条件

        时间:2023-09-23
        <i id='3eWdR'><tr id='3eWdR'><dt id='3eWdR'><q id='3eWdR'><span id='3eWdR'><b id='3eWdR'><form id='3eWdR'><ins id='3eWdR'></ins><ul id='3eWdR'></ul><sub id='3eWdR'></sub></form><legend id='3eWdR'></legend><bdo id='3eWdR'><pre id='3eWdR'><center id='3eWdR'></center></pre></bdo></b><th id='3eWdR'></th></span></q></dt></tr></i><div id='3eWdR'><tfoot id='3eWdR'></tfoot><dl id='3eWdR'><fieldset id='3eWdR'></fieldset></dl></div>

          <tbody id='3eWdR'></tbody>
        <tfoot id='3eWdR'></tfoot>

          • <small id='3eWdR'></small><noframes id='3eWdR'>

            <legend id='3eWdR'><style id='3eWdR'><dir id='3eWdR'><q id='3eWdR'></q></dir></style></legend>
                  <bdo id='3eWdR'></bdo><ul id='3eWdR'></ul>
                  本文介绍了laravel 中的急切加载关系,并带有关系条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在树中有相互关联的类别.每个类别 hasMany 子项.每个终端类别有许多个产品.

                  I have categories related to each other in a tree. Each category hasMany children. Each end category hasMany products.

                  产品也belongsToMany 不同的类型.

                  我想急切地加载带有他们的孩子和产品的类别,但我也想设置一个条件,即产品属于某种类型.

                  I want to eager load the categories with their children and with the products but I also want to put a condition that the products are of a certain type.

                  这就是我的类别模型的样子

                  This is how my categories Model looks like

                  public function children()
                  {
                      return $this->hasMany('Category', 'parent_id', 'id');
                  }
                  
                  
                  public function products()
                  {
                      return $this->hasMany('Product', 'category_id', 'id');
                  }
                  

                  产品型号

                  public function types()
                  {
                      return $this->belongsToMany(type::class, 'product_type');
                  }
                  

                  在我的数据库中,我有四个表:类别、产品、类型和产品类型

                  In my database I have four tables: category, product, type, and product_type

                  我尝试过像这样快速加载,但它加载了所有产品,而不仅仅是满足条件的产品:

                  I've tried eager loading like so but it loads all the products and not just the ones that fulfil the condition:

                  $parentLineCategories = ProductCategory::with('children')->with(['products'=> function ($query) {
                          $query->join('product_type', 'product_type.product_id', '=', 'product.id')
                              ->where('product_type.type_id', '=', $SpecificID);
                      }]])->get();
                  

                  推荐答案

                  代替当前查询,尝试这是否符合您的需要.(我根据您的评论修改了我的答案如下)

                  Instead of the current query, try if this fits your needs. (I modified my answer as follows with your comment)

                  $parentLineCategories = ProductCategory::with([
                              'children' => function ($child) use ($SpecificID) {
                                  return $child->with([
                                      'products' => function ($product) use ($SpecificID) {
                                          return $product->with([
                                              'types' => function ($type) use ($SpecificID) {
                                                  return $type->where('id', $SpecificID);
                                              }
                                          ]);
                                      }
                                  ]);
                              }
                          ])->get();
                  

                  这篇关于laravel 中的急切加载关系,并带有关系条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:检索上个月的所有行(Laravel + Eloquent) 下一篇:如何自定义 Laravel 的 DatabaseQueryBuilder(制作更好的

                  相关文章

                  最新文章

                  <tfoot id='W7EXu'></tfoot>
                    <bdo id='W7EXu'></bdo><ul id='W7EXu'></ul>

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

                    1. <legend id='W7EXu'><style id='W7EXu'><dir id='W7EXu'><q id='W7EXu'></q></dir></style></legend>