1

Im new here! I have a session on my application. Iam using ADONIS on my API. How I can attach new object, user_company when user login (session create).

    class SessionController {
  async store ({ request, response, auth }) {
    const { email, password } = request.all()


    const session = await User.findByOrFail('email', email)

    return { session }
  }
}

On my database, I have a FK to Company table. Thank you!

  • Hi Fabricio :) I hope you will find lots of helpful tips here. The best way to get an answer is to make it as easy as possible for someone to reproduce the problem (often called simply an MCVE). You can read about it here: https://stackoverflow.com/help/minimal-reproducible-example It will certainly help you get answers more quickly. – exhuma Aug 31 '20 at 17:54
  • Hi Fabricio, welcome to the community :). Could you brief a little more on where you want to add the new object. A pseudo code or comment would be much helpful for others to better understand your problem and answer. – parkourkarthik Sep 01 '20 at 07:13

1 Answers1

0

I know it's you first post... Next time, try to explain better your problem, and you will get best answers. I presume you are using a ORM, Adonis uses Lucid as ORM. With Lucid, you have two ways to do that.

1) FIRST

const session = await User.findByOrFail('email', email)
await session.load('company')

2) SECOND

const session.query().where('email', email).with('company')

Also, you can choose the fields to show.

1) FIRST

const session = await User.findByOrFail('email', email)
await session.load('company', company => {
company.setVisible(['name'])
})

2) SECOND

const session.query().where('email', email).with('company', (company) => {
    company.setVisible(['name'])
}
Rogerio Azevedo
  • 786
  • 2
  • 13
  • 32