在laravel中,当愿望清单中的项目时,它应该显示红色心形,但是当我在愿望清单中添加项目时,它会将所有产品的心形图标更改为红色,我想要产品的心形红色,哪个项目在愿望清单中
我正在返回此查询并返回true
$productData = $select_pro->get();
// Fetch wishlist data for the logged-in user and the product IDs
$wishlistData = DB::table('wishlist')
->whereIn('pid', $productData->pluck('id'))
->where('userid', $userId)
->get();
$inWishlist = $wishlistData->count() > 0;
$subImagesData = DB::table('images')->whereIn('product_id', $productData- >pluck('id'))->get();
$data = [
'products' => $productData,
'sub_images' => $subImagesData,
'inwishlist' => $inWishlist,
];
return response()->json(['data' => $data]);
我在这里将更改颜色的条件放入我的刀片文件中
var inwishlist=response.data.inwishlist;
cardHtml += '<a href="#" id="addtowishlist" class="addtowishlist" data-productid="' + productId + '">';
cardHtml += '<i class="fa fa-heart fa-2x addtowishliststyle" aria-hidden="true" style="color: ' + (inwishlist ? '#8B0000' : 'white') + '"></i>';
cardHtml += '</a>';
您可以像这样更新代码:
$wishlistData = DB::table('wishlist')
->select('pid')
->whereIn('pid', $productData->pluck('id'))
->where('userid', $userId)
->get();
$data = [
'products' => $productData,
'sub_images' => $subImagesData,
'wishlistData' => $wishlistData
];
//这是我认为的循环
var inwishlist = array_search('product.id', array_column($response.data.wishlist, 'pid'));
cardHtml += '<a href="#" id="addtowishlist" class="addtowishlist" data-productid="' + productId + '">';
cardHtml += '<i class="fa fa-heart fa-2x addtowishliststyle" aria-hidden="true" style="color: ' + (inwishlist ? '#8B0000' : 'white') + '"></i>';
cardHtml += '</a>';