laravel 5- 通过多对多搜索获取相关产品
laravel 5- get related products by search in Many To Many
Table 1: products: id,title
Table 2: features: id,name,values
Table 3:feature_product:id,product_id,values
我想在 feature_product
table 中搜索 values
时获得所有相关产品。
我这样做:
产品型号中:
public function features()
{
return $this->belongsToMany(Feature::class)->withPivot('values');
}
public function feature()
{
// ???
}
并查询搜索:
$q = 'yellow';
$query->where(function($query) use ($q)
{
$query->WhereHas('feature' , function ($query) use
($q){$query->where('values' , 'LIKE' , '%' . $q . '%' );});
}
如何在产品的相关功能中进行搜索? (并获得那些产品)
我想我必须在产品模型的这个功能上做点什么:
public function feature()
{
// ???
}
在产品型号中:
public function features()
{
return $this->belongsToMany(Feature::class)->withPivot('values');
}
public function feature()
{
// ??
}
在特征模型中:
public function products(){
return $this->belongsToMany('App\Product')->withPivot('values');
}
并查询搜索
$query->WhereHas('features' , function ($query) use ($q) {
$query->where('feature_product.values' , 'LIKE' , '%' . $q . '%' );
});
Table 1: products: id,title
Table 2: features: id,name,values
Table 3:feature_product:id,product_id,values
我想在 feature_product
table 中搜索 values
时获得所有相关产品。
我这样做:
产品型号中:
public function features()
{
return $this->belongsToMany(Feature::class)->withPivot('values');
}
public function feature()
{
// ???
}
并查询搜索:
$q = 'yellow';
$query->where(function($query) use ($q)
{
$query->WhereHas('feature' , function ($query) use
($q){$query->where('values' , 'LIKE' , '%' . $q . '%' );});
}
如何在产品的相关功能中进行搜索? (并获得那些产品)
我想我必须在产品模型的这个功能上做点什么:
public function feature()
{
// ???
}
在产品型号中:
public function features()
{
return $this->belongsToMany(Feature::class)->withPivot('values');
}
public function feature()
{
// ??
}
在特征模型中:
public function products(){
return $this->belongsToMany('App\Product')->withPivot('values');
}
并查询搜索
$query->WhereHas('features' , function ($query) use ($q) {
$query->where('feature_product.values' , 'LIKE' , '%' . $q . '%' );
});