繁体   English   中英

使用 GORM 查询 select 列

[英]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)
}

我有一个 shop_category 表。 我只想显示所有带有选定列的表,如“ID”、“名称”、“Slug”。 那么我如何才能仅使用这些列来响应表的数据。 我不想显示其他列名。

您可以使用“ 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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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