How to join table and return data in JSON format

I have two tables type Menu struct { ID int gorm:"primarykey" json:"id" CreatedAt time.Time UpdatedAt time.Time Name string json:"name" binding:"required" Unit int json:"unit" Cost float32 json:"cost" binding:"required" Type string json:"type" binding:"required" Recipe []Recipe gorm:"ForeignKey:MenuID;" json:"recipe" Transactions []Transaction gorm:"ForeignKey:MenuID;" }

and

type Transaction struct { ID int gorm:"primarykey" json:"id" CreatedAt time.Time json:"createdAt" UpdatedAt time.Time MenuID int gorm:"polymorphic:Menu;polymorphicID" json:"menuID" binding:"required" Branch string json:"branch" binding:"required" Type string json:"type" binding:"required" Channel string json:"channel" binding:"required" Price int json:"price" binding:"required" Unit int json:"unit" binding:"required" Fee float32 json:"fee" Vat float32 json:"vat" Discount float32 json:"discount" TotalPrice float32 json:"totalPrice" TotalCost float32 json:"totalCost" TotalProfit float32 json:"totalProfit" TotalProfitPercent float32 json:"totalProfitPercent" PaymentChannel string json:"paymentChannel" binding:"required" AddOn string json:"addOn" AddOns []AddOns gorm:"-" json:"addOns" Name string json:"menu" }

I use this function for query data from mariaDB db.Select("Transactions.id,Transactions.created_at,Transactions.updated_at,Transactions.menu_id,Transactions.branch,Transactions.type,Transactions.channel,Transactions.price,Transactions.unit,Transactions.fee,Transactions.vat,Transactions.discount,Transactions.total_price,Transactions.total_cost,Transactions.total_profit,Transactions.total_profit_percent,Transactions.payment_channel,Transactions.add_on, MenuList.name").Joins("inner join MenuList ON Transactions.menu_id = MenuList.id").Find(&{}model.Transactions{}).Error

  • and here is my result { "id": 3, "createdAt": "2022-09-14T18:20:08.432+07:00", "UpdatedAt": "2022-09-14T18:20:08.432+07:00", "menuID": 1, "branch": "branch 1", "type": "sales", "channel": "frontend", "price": 60, "unit": 1, "fee": 0, "vat": 0, "discount": 0, "totalPrice": 0, "totalCost": 35, "totalProfit": 25, "totalProfitPercent": 41.67, "paymentChannel": "cash", "addOn": "", "addOns": null, "menuName": "Noodle 1" },

But I want something like this { "id": 3, "createdAt": "2022-09-14T18:20:08.432+07:00", "UpdatedAt": "2022-09-14T18:20:08.432+07:00", "menuID": 1, "branch": "branch 1", "type": "sales", "channel": "frontend", "price": 60, "unit": 1, "fee": 0, "vat": 0, "discount": 0, "totalPrice": 0, "totalCost": 35, "totalProfit": 25, "totalProfitPercent": 41.67, "paymentChannel": "cash", "addOn": "", "addOns": null, "menu": { "id": 1, "name": "Noodle 1", .... // menu details } },

thank you and sorry for my bad English.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking