Search code examples
gohookgo-gormgo-fiber

How to get fiber.Ctx inside Gorm's AfterCreate() hook


I need to call some methods of fiber context like MultipartForm() and SaveFile() inside Gorm's AfterCreate() hook, how to do that?


Solution

  • You can add a field in the struct to hold fiber.Ctx in. Then call methods on this in AfterCreate hook.

    type User struct {
      c *fiber.Ctx
      <Other fields>
    }
    
    func (user *User) AfterCreate(tx *gorm.DB) (err error) {
        // user.c.MultipartForm()/ SaveFile()
        return nil
    }