简体   繁体   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)
}

I have a shop_category table.我有一个 shop_category 表。 I want to show all table with selected columns like "ID","Name","Slug" only.我只想显示所有带有选定列的表,如“ID”、“名称”、“Slug”。 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 '您可以使用“ 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