Adonis.js : How to select some attribute in query using Lucid Models

1.2k views Asked by At

I wanna select some attribute in my query.

This is my code in controllers

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

class CategoryController {

    async getCategories({request,response}) {
        try {
            let categories = await Category
                .query()
                .select('id','name')
                .with('imageables')
                .fetch()
            
            return response.json({"Categories": categories})
        } catch (error) {
            throw error
        }
    }
}

module.exports = CategoryController

This is result of query In imageables attribute, I just wanna image attribute only.

"Categories": [
    {
      "id": 1,
      "name": " tree"
      "imageables": [
          {
              "id": 1,
              "image": "tree.png",
              "type": "logo",
              "imageable_id": 1,
              "imageable_type": "categories"
          }
      ]
  },
  ...
]

and this is my form data that I hope after query.

"Categories": [
    {
      "id": 1,
      "name": " tree"
      "image": "tree.png"
  },
  ...
]
1

There are 1 answers

0
Dari4sho On

A little bit late, but I found a solution on this issue: https://github.com/adonisjs/lucid/issues/508

.with('imageables', builder => {
  builder.select('image'); //  select columns / pass array of columns
})