我有产品表,每个产品都有这样存储的标签“tag1,tag2,tag3”(字符串)现在,当我显示一个产品时,我想显示所有与当前产品相关的标签例如
product1 -> tags (tag1,tag2,tag3)product2 -> tags (tag3,tag4,tag5)product3 -> tags (tag4,tag5,tag6)
product1 -> tags (tag1,tag2,tag3)
product2 -> tags (tag3,tag4,tag5)
product3 -> tags (tag4,tag5,tag6)
例如,在产品1->中,它应该捕获产品2,因为它具有相同的标记(tag3)不幸的是,我没有写任何代码-我不知道如何实现它
fumotvh31#
是的,你可以用艰苦的方法来实现这一点。如果你使你的数据库良好,它将有助于实现任何类型的搜索。可以创建两个表
1) Products (column :- id, name) 2) product_tags(column :- id, product_id, tag_name)
使用这种结构,您可以获取任何类型的产品。如果您不想使用以上部分,请根据您的要求进行尝试。现在我假设products表中有三条记录follow:-
ID product_name product_tags1 product1 tag1,tag2,tag32 product2 tag3,tag4,tag53 product3 tag4,tag5,tag6
ID product_name product_tags
1 product1 tag1,tag2,tag3
2 product2 tag3,tag4,tag5
3 product3 tag4,tag5,tag6
现在根据您的要求,您正在展示 Product1 在网页和可能是你正在显示类似的标签产品。所以你的逻辑是this:-
Product1
$productDetail = Product:where('product_name','product1')->first(); // this query will return tags of `product1` $productDetail = json_decode(json_encode($productDetail),true); //convert to arrayif(!empty($productDetail)){ $producttags = explode(',',$productDetail['product_tags']); $productids = array(); foreach($producttags as $tag){ $getproducts = Product::whereRaw("find_in_set($tag,product_tags)")->get(); $getproducts= json_decode(json_encode($getproducts),true); if(!empty($getproducts)){ foreach($getproducts as $product){ $productids[] = $product['id']; } } } $productids = array_unique($productids); } echo "<pre>"; print_r($productids); // print the productids here and after that use `whereIn` for fetcch tag product ids
$productDetail = Product:where('product_name','product1')->first(); // this query will return tags of `product1`
$productDetail = json_decode(json_encode($productDetail),true); //convert to array
if(!empty($productDetail)){
$producttags = explode(',',$productDetail['product_tags']);
$productids = array();
foreach($producttags as $tag){
$getproducts = Product::whereRaw("find_in_set($tag,product_tags)")->get();
$getproducts= json_decode(json_encode($getproducts),true);
if(!empty($getproducts)){
foreach($getproducts as $product){
$productids[] = $product['id'];
}
$productids = array_unique($productids);
echo "<pre>"; print_r($productids); // print the productids here and after that use `whereIn` for fetcch tag product ids
希望有帮助!祝你的项目好运
1条答案
按热度按时间fumotvh31#
是的,你可以用艰苦的方法来实现这一点。如果你使你的数据库良好,它将有助于实现任何类型的搜索。可以创建两个表
使用这种结构,您可以获取任何类型的产品。
如果您不想使用以上部分,请根据您的要求进行尝试。现在我假设products表中有三条记录follow:-
现在根据您的要求,您正在展示
Product1
在网页和可能是你正在显示类似的标签产品。所以你的逻辑是this:-希望有帮助!祝你的项目好运