Get the "creator" model to use it in beforeCreate Hook


#1

Hi.

I’m working on a Picture model. My idea is to save all the pictures of each user on a folder with the user slug name.

In the Picture model I have a “url” field that is 'uploads/user_slug/picturename.jpg

When the user uploads a picture I want to use:

user.picture().create({
   name:picture_name
....
})

And then in the Picture Model I want to create a before_save hook.

class Picture extends Model {
  static boot () {
    super.boot()

    this.addHook('beforeCreate', async (instance) => {
      // Get the User Slug, create the URL and create the Picture.
    })
  }
}

How can I get the User in the Picture beforeCreate hook?

Thanks


#2

In the picture create method, you can pass a param let’s say user_id then you can read it from your instance in the before create


#3

Hi @pikitgb.
Thanks for your comment, but I thought there might be a better way. It seems not.

B/R.