nuxt-community / adonuxt-template

[Deprecated] Starter template for Nuxt.js with AdonisJS.
451 stars 61 forks source link

How to access the database #63

Open rwatts3 opened 7 years ago

rwatts3 commented 7 years ago

Greetings,

What is the best way to access the database? I believe using vuex store would be the best way to go but i'm not sure how to access the database within the store.

This question is available on Nuxt.js community (#c53)
alanaasmaa commented 6 years ago

I would use adonis to do stuff with database.

Then create some api routes that vue/nuxt will call for data. And then you can play with your data. I think that is the only (at least right) way to do it.

http://adonisjs.com/docs/3.2/adonis-blog-part3

for an example a way to get Locale

app\Controllers\Http\LangController.js

'use strict'

const Antl = use('Antl')

class LangController {
  async get ({ response }) {
    response.json({
      lang: Antl.getLocale()
    })
  }
}

module.exports = LangController

start/routes.js

const Route = use('Route')
Route.get('lang', 'LangController.get')
Route.any('*', 'NuxtController.render')

Then in vue:

<script>
import axios from '~/plugins/axios'

export default {
  methods: {
    changeLanguage (lang) {
      axios().get('/lang').then((response) => {
        console.log(response)
      }).catch((error) => {
        console.log(error)
      })
    }
  }
}
</script>