渴望使用pivot加载嵌套关系

时间:2014-07-28 12:48:56

标签: database laravel eloquent

我目前有4张桌子:

accessories 
id 

products 
id

product_accessory 
id 
product_id
accessory_id 

product_accessory_adaptor
id 
product_accessory_id 

将它们映射到Eloquent模型给了我

自定义枢轴模型:

class ProductAccessory extends Pivot {
   protected $table = 'product_accessory';

   public function product()
   {
      return $this->belongsTo('Product');
   }

   public function accessory()
   {
     return $this->belongsTo('Accessory');
   }

   public function adaptors() {
     return $this->hasMany('Adaptor', 'product_accessory_id'); 
   } 
}

产品和附件模型

class Accessory extends Eloquent {

   public function products()
   {
      return $this->belongsToMany('Product', 'product_accessory', 'accessory_id', 'product_id')->withPivot();
   }

   public function newPivot(Eloquent $parent, array $attributes, $table, $exists) 
   {
      if ($parent instanceof Product) {
          return new ProductAccessory($parent, $attributes, $table, $exists);
      }
      return parent::newPivot($parent, $attributes, $table, $exists);
   }

   public function adaptors()
   {
      return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'accessory_id', 'product_accessory_id');
   }
}

class Product extends Eloquent {

   public function accessories()
   {
      return $this->belongsToMany('Accessory', 'product_accessory', 'product_id', 'accessory_id')->withPivot();
   }

   public function newPivot(Eloquent $parent, array $attributes, $table, $exists) 
   {
      if ($parent instanceof Accessory) {
          return new ProductAccessory($parent, $attributes, $table, $exists);
      }
      return parent::newPivot($parent, $attributes, $table, $exists);
   }

   public function adaptors()
   {
      return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'product_id', 'product_accessory_id');
   }
}

适配器型号:

class Adaptor extends Eloquent {

   protected $table = 'product_accessory_adaptor';

   public function productAccessory() {
      return $this->belongsTo('ProductAccessory');
   }
}

这就是我需要的。急切地为所有产品加载带有属于其枢轴的适配器的配件。我尝试过类似的东西,但我不知道如何传递透视ID

产品型号中的

public function accessories()
{
    return $this->belongsToMany('Compatibility\Accessory', 'product_accessory', 'product_id', 'accessory_id')
                ->withPivot('id', 'accessory_disclaimer')
                ->withTimestamps()
                ->with(['adaptors' => function ($q) {
                    $q->where('product_accessory_id', $this->pivot->id);                       
                }]);
}

有没有人知道如何做到这一点?我应该用$ this-> pivot-> id?

替换什么

1 个答案:

答案 0 :(得分:1)

无需急切加载与枢轴模型相关的任何内容。它不会以这种方式工作,所以我建议:

$products->load('ProductAccessory.accessory', 'ProductAccessory.adaptors');

// then you access related accessory and adaptors through the pivot model
$product = $products->first();

$product->productAccessory->accessory;
$product->productAccessory->adaptors;

// Product model
public function productAccessory()
{
  return $this->hasMany('ProductAccessory');
}

注意:ProductAccessory应该延伸EloquentModel),而不是Pivot - 就像我在上一个问题中说明了我的答案。

这是将它放在一个地方的方法,即。获取相关配件和相关适配器。

否则你需要这样的东西:

$product->accessory; // accessory model, so far so good
$product->adaptors; // all adaptors, not so good anymore

// to get adaptors for the accessory we need:
$accessories = $product->adaptors()->where('product_accessories.id', $product->accessory->pivot->id)->get();

不太方便..