Laravel 5.1 - 通过枢轴显示相关产品 table
Laravel 5.1 - showing related products through pivot table
我被困在这条路上太久了,无法自己解决,所以这是我的问题:
我的数据库被衣服填满了。我想根据颜色显示相关的衣服。
我的人际关系:
(Product.php)
public function colors(){
return $this->belongsToMany('App\Color', 'colors_products', 'FK_product', 'FK_color');
}
(Color.php)
public function products(){
return $this->belongsToMany('App\Product', 'colors_products', 'FK_color', 'FK_product');
}
所以每个 product
有多个 colors
。我想在同一页面上显示相关作品。所以如果我正在看一件颜色为 "red" 和 "black" 的衣服,我想展示标有 "red" and/or "black" 的两件相关衣服。所以它不必既是红色又是黑色。
我怎样才能让所有衣服都像我正在看的那件衣服一样具有一种(有时是多种)颜色?
更新
Product::with('colors')->whereIn('id', $arColors)->get();
是解决方案的一部分。当我有两种颜色(红色和黑色)的产品时,我看到相关产品只有黑色或只有红色。但是当我看一个只有黑色的产品时,相关的多个颜色的项目没有显示。
试试这个,希望对你有帮助。
Color::with('products')->whereIn('color', ['red', 'black'])->get();
这将为您提供颜色以及与之关联的所有产品。
更新:
根据你的更新,你可以试试这个。
Product::with(['color' => function($q) use ($colorArray) {
$q->whereIn('color', $colorArray); }])
->has('color')
->get();
我没有运行这个,但希望能工作。
如果您想使用关系并且愿意使用视图(或使用支持它的数据库),您可以创建这样的视图:
create
view related_products
as
select distint
a.FK_product as FK_product,
b.FK_product as FK_related_product
from
colors_products as a
join colors_products as b
on (a.FK_color = b.FK_color and a.FK_product <> b.FK_product);
这应该加入具有至少一种共同颜色的所有产品。
现在您可以向模型添加关系,如下所示:
public function relatedProducts()
{
return $this->belongstoMany('App\Product', 'related_products', 'FK_related_product');
}
NULL 的答案很接近。我想你想为此使用 whereHas() 。鉴于您的颜色数组很容易从您的活动产品中获得,假设它被称为 $color_ids.
要加载与其中一种颜色相匹配的所有其他产品,请尝试以下操作:
$related_products = Product::whereHas('colors', function($query) use ($color_ids) {
$query->whereIn('id', $color_ids);
})->get();
whereIn() 记录在此处:http://laravel.com/docs/5.1/queries#where-clauses
whereHas() 记录在此处:http://laravel.com/docs/5.1/eloquent-relationships#querying-relations
我被困在这条路上太久了,无法自己解决,所以这是我的问题:
我的数据库被衣服填满了。我想根据颜色显示相关的衣服。
我的人际关系:
(Product.php)
public function colors(){
return $this->belongsToMany('App\Color', 'colors_products', 'FK_product', 'FK_color');
}
(Color.php)
public function products(){
return $this->belongsToMany('App\Product', 'colors_products', 'FK_color', 'FK_product');
}
所以每个 product
有多个 colors
。我想在同一页面上显示相关作品。所以如果我正在看一件颜色为 "red" 和 "black" 的衣服,我想展示标有 "red" and/or "black" 的两件相关衣服。所以它不必既是红色又是黑色。
我怎样才能让所有衣服都像我正在看的那件衣服一样具有一种(有时是多种)颜色?
更新
Product::with('colors')->whereIn('id', $arColors)->get();
是解决方案的一部分。当我有两种颜色(红色和黑色)的产品时,我看到相关产品只有黑色或只有红色。但是当我看一个只有黑色的产品时,相关的多个颜色的项目没有显示。
试试这个,希望对你有帮助。
Color::with('products')->whereIn('color', ['red', 'black'])->get();
这将为您提供颜色以及与之关联的所有产品。
更新: 根据你的更新,你可以试试这个。
Product::with(['color' => function($q) use ($colorArray) {
$q->whereIn('color', $colorArray); }])
->has('color')
->get();
我没有运行这个,但希望能工作。
如果您想使用关系并且愿意使用视图(或使用支持它的数据库),您可以创建这样的视图:
create
view related_products
as
select distint
a.FK_product as FK_product,
b.FK_product as FK_related_product
from
colors_products as a
join colors_products as b
on (a.FK_color = b.FK_color and a.FK_product <> b.FK_product);
这应该加入具有至少一种共同颜色的所有产品。
现在您可以向模型添加关系,如下所示:
public function relatedProducts()
{
return $this->belongstoMany('App\Product', 'related_products', 'FK_related_product');
}
NULL 的答案很接近。我想你想为此使用 whereHas() 。鉴于您的颜色数组很容易从您的活动产品中获得,假设它被称为 $color_ids.
要加载与其中一种颜色相匹配的所有其他产品,请尝试以下操作:
$related_products = Product::whereHas('colors', function($query) use ($color_ids) {
$query->whereIn('id', $color_ids);
})->get();
whereIn() 记录在此处:http://laravel.com/docs/5.1/queries#where-clauses
whereHas() 记录在此处:http://laravel.com/docs/5.1/eloquent-relationships#querying-relations