How to convert raw queries into JSON?

Hi devs, im trying to get the result of this query and use on my frontend. I’m using Edge template. Its easy when im using Lucid ORM:

const **telefones** = await Telefone.all()
return view.render('telefones', {
   
      title: 'Telefones',
      **telefones**: **telefones.toJSON()**      
   })

But what to do this when we’re using raw queries?

TelefoneController.js

async listar ({ request, response, view }) {
   
const **telefones** = await Database.raw(`
    Select t.voip,t.ramal,t.atendente, d.nome from telefones t 
        join departamentos d on d.id = t.id_departamento
    where d.nome = 'CPD'
                `) 

return view.render('telefones', {   
      title: 'Telefones',    
   })
  }
1 Like

Someone answered your question: Database.raw() how to serialize result to JSON

1 Like
async listar ({ request, response, view }) {
   
const telefones = await Database.raw(`
    Select t.voip,t.ramal,t.atendente, d.nome from telefones t 
        join departamentos d on d.id = t.id_departamento
        where d.nome = 'CPD'`)
    .then(result => result.rows)


return view.render('telefones', {   
      title: 'Telefones',
      telefones
   })
}