How to remove a key ( field ) from Lucid model


#1

Hi

I’ve defined a hook for my lucid model like this

static boot () {
	super.boot()
	this.addHook('afterFind', async (offer) => {
		offer.farsi_created_at = Functions.en_date_2_fa_date(offer.created_at);
	});
}

which adds a farsi_created_at property to object after finding it.

but when I want to save the model, it returns this error

ER_BAD_FIELD_ERROR: Unknown column 'farsi_created_at' in 'field list' 

which seems true because this field which I’ve added in find hook doesn’t exist in the offers table

I tried to delete the added property in beforeUpdate hook like this:

this.addHook('beforeUpdate', async (offer) => {
	delete offer['farsi_created_at']
})

but I still get the same error and property still exists in the object of model.

How can I delete properties that I added to the object before save ?


#2

Getters or Computed properties


#3

It’s another way of implementing but for, sync only, code.
I want to know how can I delete a key from Lucid Model


#4

Why want you add a property and not saving it while you can create a virtual property which will do what you want automatically…?


#5

Getters are synchronous, which means you cannot run asynchronous code inside them. You must use hooks for that.

because getters cannot run async code and I want to run async code.
That date is just an example in this question.

and the reason Why I want is something outside the scope of this question.


#6

Simply add it to the sideloaded object that each model has.

modelInstance.$sideLoaded.myprop = value