Eager load specific fields


#1

Is it possible to eager load just some specific fields of the related model?


#2
  async index ({ request, response, view }) {
    const structures = await Structure
      .query()
      .with('structureType')
      .with('reviews')
      .fetch()
    return structures
  }

Let’s say i just want a single field from ‘reviews’, how could I do that?


#3

Hey @iacapuca! :wave:

The method with() take a callback for the second argument.
You can modify the query inside it.

const structures = await Structure.query()
  .with('structureType')
  .with('reviews', (builder) => {
    builder.select(['id', ...])
  })
  .fetch()

#4

That worked just fine!

Does Adonis have something like a Laravel/Symphony Transformer? If so, how could I use it withing my controller?


#5

You must explain what’s the job of the transformer and what exactly you are tying to do. Expecting others to search for transformers and then help you is not nice


#6

I’m sorry, I dont mean that, I was just wondering if Adonis had Data Transformers like Laravel.


#7

@iacapuca This could help :slight_smile:

It works perfect on few of my projects