简体   繁体   中英

Query select columns with GORM

func GetShopCategory(c *fiber.Ctx) error {
    var shopCategories []model.ShopCategory
    err := model.DB.Select("ID","Name","Slug").Find(&shopCategories)
    if err.Error != nil {
        return c.SendStatus(fiber.StatusNoContent)
    }
    return c.JSON(shopCategories)
}

I have a shop_category table. I want to show all table with selected columns like "ID","Name","Slug" only. so how can I response table's data with only these column. i don't wanna show other columns name.

You can accomplish that with ' Smart Select Fields '

type APIShopCategory struct {
    ID uint
    Name string
    Slug string
}


func GetShopCategory(c *fiber.Ctx) error {
    var shopCategories []APIShopCategory
    err := model.DB.Model(&model.ShopCategory{}).Find(&shopCategories)
    if err.Error != nil {
        return c.SendStatus(fiber.StatusNoContent)
    }
    return c.JSON(shopCategories)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM