Skip to content

MZanggl/adonis-lucid-update-or-create

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Installation

npm i adonis-lucid-update-or-create --save

Registering provider

Make sure to register the update or create provider inside start/app.js

const providers = [
  "adonis-lucid-update-or-create/providers/UpdateOrCreateProvider"
]

Usage

First add the trait to the model.

const Model = use("Model")

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

    this.addTrait("@provider:Lucid/UpdateOrCreate")
  }
}

Finally use the method as follows

const Rating = use("App/Models/Rating")

Rating.updateOrCreate(
  {
    user_id: 18,
    article_id: 23
  },
  { rating: 5 }
)

If you use transactions, you can pass trx as the third argument.

About

Implements Laravel's updateOrCreate method as a trait in AdonisJs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published