Implements Laravel's updateOrCreate method as a trait in AdonisJs
npm install adonis-lucid-update-or-createbash
npm i adonis-lucid-update-or-create --save
`
$3
Make sure to register the update or create provider inside start/app.js
`javascript
const providers = [
"adonis-lucid-update-or-create/providers/UpdateOrCreateProvider"
]
`
$3
First add the trait to the model.
`javascript
const Model = use("Model")
class Rating extends Model {
static boot() {
super.boot()
this.addTrait("@provider:Lucid/UpdateOrCreate")
}
}
`
Finally use the method as follows
`javascript
const Rating = use("App/Models/Rating")
Rating.updateOrCreate(
{
user_id: 18,
article_id: 23
},
{ rating: 5 }
)
``