New model extending from an existing one


#1

Hello, someone can tell me how I can create models from an already existing one, for example in Rails I use this

class Cleaner <User
default_scope {where (role_id: 6)}

has_many: branches
end


#2

Yeah I will try to explain you with an example:

First we have this model:

'use strict'

const Model = use('Model')

class Post extends Model {
    

    user() {
        return this.belongsTo('App/Models/User')
    }
}

module.exports = Post
  • After we need the Demo model so we create it in appName/models

  • Now the current Demo model needs require the Post model so remember do it in this way ./nameModel

  • After we create a Demo class that extends from Post

  • Finally we need to export this module as follows module.exports = Demo

const Post = require('./Post')

class Demo extends Post 
{
    static get table() {
        return 'posts'
    }
}

module.exports = Demo

Now on your controller you invoke it in this way

const Demo = use('App/Models/Demo')

Now you can use it to make queries to your database in this way

const respuesta = await Demo.all()
return respuesta

#3

thank you very much, it worked very well