【问题标题】:Laravel multiple models relationshipLaravel 多模型关系
【发布时间】:2021-03-03 09:50:06
【问题描述】:

我的应用中有以下 3 个模型:

  • 产品
  • 子类别
  • 类别

产品属于属于某个类别的子类别。每个产品只属于一个子类,一个子类可以有N个产品但只属于一个类,一个类也可以有多个子类。

此时我可以从一个产品转到他的子类别并跳转到主类别,这里是 tinker 控制台的示例:

>>> $product = App\Models\Product::Find(1);
=> App\Models\Product {#4156
     id: "1",
     name: "Jersey",
     description: "Black Jersey",
     type_id: "1",
     brand_id: "1",
     subcategory_id: "1",
     created_at: "2021-03-03 00:46:26",
     updated_at: "2021-03-03 00:46:26",
   }
>>> $product->subcategory;
=> App\Models\Subcategory {#3684
     id: "1",
     category_id: "1",
     name: "Top",
     created_at: "2021-03-03 00:44:31",
     updated_at: "2021-03-03 00:44:31",
   }
>>> $product->subcategory->category;
=> App\Models\Category {#4246
     id: "1",
     category: "Clothing",
     created_at: "2021-03-03 00:42:14",
     updated_at: "2021-03-03 00:42:14",
   }
>>> 

但是当我尝试从一个类别到另一个子类别并获得所有具有相同子类别的产品时,我会收到此错误:

>>> $category = App\Models\Category::Find(1);
=> App\Models\Category {#4299
     id: "1",
     category: "Clothing",
     created_at: "2021-03-03 00:42:14",
     updated_at: "2021-03-03 00:42:14",
   }
>>> $category->subcategory;
=> Illuminate\Database\Eloquent\Collection {#4305
     all: [
       App\Models\Subcategory {#4300
         id: "1",
         category_id: "1",
         name: "Top",
         created_at: "2021-03-03 00:44:31",
         updated_at: "2021-03-03 00:44:31",
       },
     ],
   }
>>> $category->subcategory->product;
Exception with message 'Property [product] does not exist on this collection instance.'
>>> 

以下是我使用的模型:

产品:

public function subcategory(){
        return $this->belongsTo(Subcategory::class);
    }

类别:

public function subcategory(){
        return $this->hasMany(Subcategory::class);
    }

子类别:

public function category(){
        return $this->belongsTo(Category::class);
    }

    public function product(){
        return $this->hasMany(Product::class);
    }

我希望能够获取属于同一类别(即属于不同子类别但属于同一类别)或所有相同子类别的所有产品。

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    您需要使用hasManyThrough 关系。

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Category extends Model {
    
        /**
         * Get all of the products for the project.
         */
    
        public function products()
        {
            return $this->hasManyThrough(Product::class, Subcategory::class);
        }
    }
    

    你可以把这个关系当作

    $category = App\Models\Category::Find(1);
    
    $products = $category->products
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您可以使用hasManyThrough 关系来获取一个类别的所有产品。将此添加到您的类别模型中。

      public function products()
      {
          return $this->hasManyThrough(Product::class, Subcategory::class);
      }
      

      在您当前的方法中,您将获得一个集合表单$category-&gt;subcategory,您必须遍历集合以获取每个子类别的产品。所以对你来说最好的选择是使用hasManyThrough关系,你可以简单地调用$category-&gt;products,它将返回该类别的所有产品。

      【讨论】:

      • 是的,我终于工作了,感谢您的支持
      【解决方案3】:

      如果您使用 Eloquent 获得当前产品:

      $product= Product::find(5);
      

      你可以用这个:

      $result = Product::whereIn('subcategory_id', function($query){
          $query->select('subcategory_id')
          ->from(with(new Category)->getTable())
          ->where('id', $product->subcategory->category->id);
      })->where('subcategory_id','!=',$product->subcategory->id)->get();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-26
        • 1970-01-01
        • 2018-12-12
        • 2017-05-25
        • 2018-04-15
        • 1970-01-01
        • 2019-04-04
        相关资源
        最近更新 更多