Laravel 自定义数据透视表关系和预加载?

2024-04-10

我在为我的一个项目创建架构/模型时遇到问题,希望在这里获得一些帮助。

我目前有 3 个表: Accessories 、 products 和数据透视表 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');
}

现在的问题是我需要添加另一种产品类型“适配器”,它最终取决于数据透视表关系,也就是说,适配器需要与产品和配件相关......

UPDATE这是我当前的product_accessory_adaptor表的样子

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

这样,我可以拥有许多与产品和配件相关的适配器。我的问题是如何用雄辩的方式建模这种关系?

这是我现在所拥有的:

自定义枢轴模型:

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

Update现在架构和模型已设置。然而,使用 hasManyThrough 关系存在一些问题。此外,在这种情况下,有什么方法可以对枢轴关系(即适配器)进行急切加载?

Note当我在产品或配件模型上调用适配器()时发生的错误是Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model, none given, called in /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 872 and defined


这就是你的支点:

<?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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel 自定义数据透视表关系和预加载? 的相关文章

随机推荐

  • 如何使 is_arithmetic::value 为 true?

    我的想法是 我有一个函数可以对输入进行算术运算 所以可能是这样的 include
  • Django 中的 Meta 到底是什么?

    我想简单地知道 Django 中的 Meta 类是什么以及它们的作用 from django db import models Class Author models Model first name models CharField ma
  • mod_rewrite - PHP:$_GET 不完整

    我有以下 mod rewrite Options FollowSymLinks RewriteEngine On RewriteRule register index php page register 网址如下所示 http domain
  • 如何向下旋转 pandas 数据框中每一行的行值? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我的数据框 Name Percent Subject1 Subject2 ramesh 85 Maths Science ram 42
  • 如何在 Jar 文件中获取 Unicode

    我编写了一个从程序外部调用 Unicode 的程序 我使用的是 Windows XP 和 Eclipse 当我在 IDE 中运行该程序时 它显示 Unicode 但是当我将其导出为 jar 文件时 我无法读取 Unicode 它显示为方框
  • Google 计算引擎负载平衡无法正确路由

    我是 Google 计算引擎的新手 我尝试设置网络负载平衡 拥有 2 个虚拟机来提供网页服务 例如 我有 2 个虚拟机 app1 和 app2 都有 apache 服务器并提供简单的网页 两个虚拟机都运行 Red Hat Enterpris
  • 绑定参数太多。提供了 5 个参数,但该语句需要 4 个参数

    执行下面的函数时 我得到上面的 IllegalArgumentException 我不明白的是 当我运行调试器时 values变量显然只包含 4 个参数 正如它应该的那样 So 1 这个神秘的第五个论点从何而来 2 我应该如何发现这个错误
  • C# 屏幕截图全窗口

    我正在尝试使用 NET Framework 编写一个控制台应用程序 我想截图我的屏幕 我已经使用过其他答案 如下所示 https stackoverflow com a 24879511 9457997 https stackoverflo
  • 在 32 位和 64 位 C# 世界中使用 System.Data.SQLite 的选项

    我了解为什么在 32 位和 64 位版本中提供 System Data SQLite dll 因此 我们不要纠缠于此 继续前进 由于采用这种方式 纯 C 开发似乎变得更加困难 需要做出 3 个选择 是只支持32位并强制有托管 编译 x86
  • jQuery - 如何编写“如果不等于”(与 == 相反)

    我需要反转以下代码 如果宽度不是 500px 如何使动画运行 image div not this each function if this css width 500px this animate width 250px 500 fun
  • 查询以比较带时间的日期和不带时间的日期 - python 使用 access db

    我需要帮助来创建查询来比较带时间的日期和不带时间的日期 我正在使用带有 access db pypyodbc 的 python 在数据库中 我有一个包含日期 时间 包括时间 的列 在Python中 我有一个日期时间对象 没有时间 我想编写一
  • 为页面创建 Like-Gate [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我已经创建了一个 Facebook 页面 我想为其添加一个所谓的 Like Gate 问题是该页面带有时间线 并且到目前为止我找到的每个教
  • Conda无法激活环境

    Microsoft Windows Version 6 1 7601 Copyright c 2009 Microsoft Corporation All rights reserved C Users gt conda env list
  • 具有多个鉴别器值的 Hibernate 子类

    您好 我有一个子类需要覆盖多个鉴别器值 就像是 DiscriminatorValue 值 ACT DNR ATT 会让我变得完美 我们有现有的数据 其中多个鉴别器可以映射到一个类 因为它们与我们的系统将视为同一事物的类型相似 您可以使用判别
  • jQuery 淡入/淡出 div 到不同的 div?

    当您单击链接或按钮时 是否可以让 div 淡出 然后在同一位置淡入具有不同内容的不同 div 显然会使用 fadeIn and fadeOut 函数 但我不确定所有代码会是什么样子 特别是定位 以及在同一页面上执行两次的能力 如果您想淡出一
  • 如果我的文件中有翻译,如何在 DSpace 中翻译或替换主题术语

    如果语言切换 我想翻译我正在维护的 DSPace 实例中 item view xsl 中显示的主题 MeSH 术语 以前我使用下面的代码 我将其添加到XSLUtils java类 来查找 Babelmesh 站点并动态翻译它 public
  • 从 groovy 方法调用顶级函数

    我认为这有一个简单的答案 但我的网络搜索找不到它 如果我有以下内容 ideone http ideone com HVR89L def f class C public h f x new C h 此操作失败并出现以下错误 No signa
  • 在 OS X 下将 ImageMagick 编译为 64 位?

    我正在尝试安装moddims http code google com p moddims 在 OS X 上 请参阅上一个问题 https stackoverflow com questions 1185106 how do i confi
  • 用于沿大矩阵对角线插入 2x2 矩阵的代码的向量化

    我正在尝试沿大矩阵 例如 10x10 的对角线对小矩阵 2x2 进行元素插入 添加重叠值 并且仅将小矩阵插入到可以完全放入大矩阵的位置 我已经使用 for 循环实现了这一点 但我很好奇该过程是否可以矢量化 function M TestDi
  • Laravel 自定义数据透视表关系和预加载?

    我在为我的一个项目创建架构 模型时遇到问题 希望在这里获得一些帮助 我目前有 3 个表 Accessories products 和数据透视表 Product accessory