I have a function that outputs all Product data to the data array, but I also need to display 5 products from the same category in the relatives array.
Function:
func GetProductsById(c *gin.Context) {
var Product models.Products
Products := []models.Products{}
config.DB.Where("cat_id", func(tx *gorm.DB) *gorm.DB {
return config.DB.Where("id=?", c.Query("prod_id")).Select("cat_id").First(&Product)
}).Find(&Products)
if err := config.DB.Where("id=?", c.Query("prod_id")).First(&Product).Error; err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
} else {
c.JSON(http.StatusOK, gin.H{
"data": []models.Products{Product},
"relatives": &Products})
}
}
I tried to do it myself, but I can't access the product category from the data array to show 5 similar products.
And i have error:
cannot convert 0xa182e0 to Int8