gpt4 book ai didi

database - Laravel 自定义数据透视表关系和急切加载?

转载 作者:太空狗 更新时间:2023-10-30 01:50:47 26 4
gpt4 key购买 nike

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

我目前有 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');
}

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

更新这是我当前的 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');
}

这样,我就可以拥有多个与产品和配件相关的适配器。我的问题是如何以 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 关系存在问题。此外,在这种情况下,有什么方法可以为枢轴关系(即适配器)进行预加载?

注意当我在 Product 或 Accessory 模型上调用 adaptors() 时发生的错误是 传递给 Illuminate\Database\Eloquent\Relations\Pivot::__construct() 的参数 1 必须是 Illuminate\Database\Eloquent\Model 的一个实例,没有给出,在/vendor/laravel/framework/src/Illuminate/中调用Database/Eloquent/Model.php 872行定义

最佳答案

这是你的支点:

<?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

关于database - Laravel 自定义数据透视表关系和急切加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24857522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com