我有一些表。Laravel 5.2条件查询在多对多关系中
products(product_id(pk),title)
product_tag(id(pk),tag_id(fk),product_id(fk))
tags(tag_id(pk),tag_name)
我也有其他产品有许多关系的表。 这样我在产品模型中建立多对多的关系。
public function productTags()
{
return $this->belongsToMany("App\Model\TagModel","product_tags","product_id","tag_id");
}
我想要的是按关键字搜索。假设我有一些关键字,如
['shirts','pant']
关键字数组可能包含单个项目或多个项目。
我希望这个关键字与tag_name匹配,并通过product_tags表检索那些与tag_id关联的产品,以及所有这类产品的标题都是这样的关键字。
例子: 产品表
product_id title
1 half sleeve shirt
2 full sleeve shirt
3 pants
4 women jeans
product_tag表
product_id tag_id
1 1
2 1
3 2
4 3
标签表
tag_id tag_name
1 mens
2 pant
3 women
现在,如果我的搜索关键词阵列像
['shirt','pant']
结果应该返回产品表中的前三种产品,因为它们匹配。前两个产品直接与关键字匹配,第三个产品与tag_id匹配。
我也有其他表与产品表有多对多关系。