Laravel自定义枢轴表关系和急切负载? [英] Laravel Custom Pivot Table relationship and eager loading?

查看:155
本文介绍了Laravel自定义枢轴表关系和急切负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为我的一个项目创建模式/模型时遇到问题,并希望在此处获得一些帮助。

I am having problems with creating a schema / model for one of my projects and would like to get some help here.

我目前有3张表:配件,产品和数据透视表product_accessory

I have 3 tables currently : accessories , products and a pivot table product_accessory

<?php 

Schema::create('accessories', function(Blueprint $table)
{
    $table->increments('id');
}

Schema::create('products', function(Blueprint $table) 
{
    $table->increments('id');
}

Schema::create('product_accessory', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('product_id')->unsigned();
    $table->integer('accessory_id')->unsigned();
    $table->foreign('product_id')->references('id')->on('products');
    $table->foreign('accessory_id')->references('id')->on('accessories');
}

添加另一个产品类型adapters,这将最终取决于枢轴表关系,即适配器需要涉及产品和附件...

Now problem is I need to add another product type 'adaptors' that would ultimately depend on the pivot table relation, that is, adaptors need to relate to both a product and accessory...

UPDATE
这里是我当前的product_accessory_adaptor表是

UPDATE Here is how my current product_accessory_adaptor table is

Schema::create('product_accessory_adaptor', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('product_accessory_id')->unsigned();
    $table->foreign('product_accessory_id')->references('id')->on('product_accessory');
}

这样,我可以有许多适配器与一个产品和附件。

This way, i can have many adaptors relating to a product and accessory. My question is how do i model this relation in 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');
   }
}

更新
现在设置模式和模型。但是使用hasManyThrough关系有问题。此外,在这种情况下,对于枢纽关系,即适配器,任何方式都急于加载?

Update Now the schema and model is setup. However there are issues with using hasManyThrough relations. In addition, any way to do eager loading in this case for the pivot relation , i.e Adaptor?

注意
当我对产品或附件模型上的adapters()调用
时,发生调用1参数1传递给Illuminate \Database\Eloquent\Relations\Pivot :: __ construct()必须是Illuminate \Database\Eloquent\Model的实例,没有给出,在第872行中的/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php中调用并定义

推荐答案

这是您的支柱:

<?php

use Illuminate\Database\Eloquent\Model as Eloquent;

// you don't need to call it ..Pivot, just my suggestion
class ProductAccessory extends Eloquent {

  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');
  }
}

// Product model
public function adaptors()
{
    return $this->hasManyThrough(
      'Adaptor', 'ProductAccessoryPivot', 'product_id', 'product_accessory_id'
    );
}

// Accessory model
public function adaptors()
{
    return $this->hasManyThrough(
      'Adaptor', 'ProductAccessoryPivot', 'accessory_id', 'product_accessory_id'
    );
}

现在使用示例:

$product = Product::first();

$product->adaptors; // collection of all adaptors for given product

$product->adaptors->first()->accessory; // accessory for single adaptor

$product->accessories; // collection of accessories, each with your custom pivot, so:

$product->accessories->first()->adaptors; // collection of adaptors for given product-accessory pair

... and more, try it

这篇关于Laravel自定义枢轴表关系和急切负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆