英文:
Relationship queries in a three-way PivotTable
问题
我需要获取与三面摘要表一起工作的任务,但我无法获取特定产品的所有属性和它们的值。
我有三个模型Product,Property和PropertyValue。
Product模型:
public function properties(): BelongsToMany
{
return $this->belongsToMany(Property::class, 'product_property', 'product_id', 'property_id')
->with('values')
->withPivot('property_value_id')
->orderBy('order');
}
public function values(): BelongsToMany
{
return $this->belongsToMany(PropertyValue::class, 'product_property', 'product_id', 'property_value_id');
}
Property模型:
public function products(): BelongsToMany
{
return $this
->belongsToMany(Product::class, 'product_property', 'property_id', 'product_id');
}
public function values(): BelongsToMany
{
return $this->belongsToMany(PropertyValue::class, 'product_property','property_id','property_value_id')->withPivot('product_id');
}
PropertyValue模型:
public function properties(): BelongsToMany
{
return $this
->belongsToMany(Property::class, 'product_property','property_id','property_value_id');
}
public function products(): BelongsToMany
{
return $this
->belongsToMany(Product::class, 'product_property','property_value_id','product_id');
}
Pivot表迁移 product_property:
public function up()
{
Schema::create('product_property', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('product_id')->constrained()->onDelete('cascade');
$table->foreignId('property_id')->constrained()->onDelete('cascade');
$table->foreignId('property_value_id')->constrained()->onDelete('cascade');
});
}
我可以获取Product,然后获取其属性Property:
$product->properties
或者只获取其PropertyValue属性的值:
$product->values
但是,我无法获取链式结构:
Product->This property->The values of these properties for the selected product.
如果我尝试像这样做:
$product->properties()->first()->values
然后我会获取选定Property在中间表中的所有PropertyValue,而不是特定选定的Product。
由于不能更改数据库,我的任务变得更加复杂。请告诉我如何在不更改中间表的情况下获取这样的链:
Product->This property->The values of these properties for the selected product.
英文:
I had the task of working with a three-sided summary table, but I can not get all the properties and their values for a particular product.
I have three models Product, Property and PropertyValue.
Model Product:
public function properties() :BelongsToMany
{
return $this->belongsToMany(Property::class,'product_property', 'product_id', 'property_id')
->with('values')
->withPivot('property_value_id')
->orderBy('order');
}
public function values() :BelongsToMany
{
return $this->belongsToMany(PropertyValue::class,'product_property', 'product_id', 'property_value_id');
}
Model Property:
public function products() :BelongsToMany
{
return $this
->belongsToMany(Product::class, 'product_property', 'property_id', 'product_id');
}
public function values() :BelongsToMany
{
return $this->belongsToMany(PropertyValue::class, 'product_property','property_id','property_value_id')->withPivot('product_id');
}
Model PropertyValue:
public function properties() :BelongsToMany
{
return $this
->belongsToMany(Property::class, 'product_property','property_id','property_value_id');
}
public function products() :BelongsToMany
{
return $this
->belongsToMany(Product::class, 'product_property','property_value_id','product_id');
}
Pivot table migration product_property:
public function up()
{
Schema::create('product_property', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('product_id')->constrained()->onDelete('cascade');
$table->foreignId('property_id')->constrained()->onDelete('cascade');
$table->foreignId('property_value_id')->constrained()->onDelete('cascade');
});
}
I can get Product and then get either its properties Property
$product->properties
or only the values of its PropertyValue properties
$product->values
However, I can't get the chain:
Product->This property->The values of these properties for the selected product.
If I do something like
$product->properties()->first()->values
then I get all the PropertyValue selected Property in the pivot table, not for the specifically selected Product.
My task is complicated by the fact that I cannot make any changes to the database.
Please tell me how to get a chain without changing the pivot table
Product->This property->The values of these properties for the selected product.
答案1
得分: 0
尝试类似以下方式:
$property = $product->properties()->first();
$values = $property->values()->where('product_id', $product->id)->get();
或者类似以下方式:
$property = $product->properties()->first();
$values = $property->values()
->join('product_property', 'product_property.property_value_id', '=', 'property_values.id')
->where('product_id', $product->id)
->get();
英文:
Try something like this :
$property = $product->properties()->first();
$values = $property->values()->where('product_id', $product->id)->get();
Or like this :
$property = $product->properties()->first();
$values = $property->values()
->join('product_property', 'product_property.property_value_id', '=', 'property_values.id')
->where('product_id', $product->id)
->get();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论