How to display data (Select *)

Good morning, sir,
I’m a beginner on this framework, however, I can look at the documentation, but I don’t really see how Select * form [table] can be used.

Ex for my table:

Thank,

2 Likes

hi @DevWeb

if you use lucid do like this

const user = use('App/Model/User')

class MyController {
      async myMethod(){
         const user = await User.all()
          return user
     }


}


this link its help you https://adonisjs.com/docs/4.1/lucid

if you use Query Builder do like this


class MyController {
      async myMethod(){
         const user =await Database.from('users').first()
          return user
     }


}

this link its help you https://adonisjs.com/docs/4.1/query-builder

3 Likes

Hey,
Thanks, so for display data (Ex: all “username”), for Database

Re,
My controller

'use strict'
const Database = use('Database')

class GettingStartedController {
  index ({ request, response, view, antl }) {
	const navigations = await Database.from('navigation').first()
    return view.render('getting-started/index', { locales: antl.availableLocales() }, { navigations: navigations.toJSON() })
  }
}

module.exports = GettingStartedController

What the problem ?

My .edge file

@each(navigation in navigations)
          <li><a href="{{ navigation.url }}">{{ navigation.name }}</a></li>
		  @endeach

Hello @DevWeb
Change :

return view.render('getting-started/index', 
            { locales: antl.availableLocales() }, 
            { navigations: navigations.toJSON() })

to

return view.render('getting-started/index', {
    locales: antl.availableLocales(),
    navigations: navigations.toJSON()
  });

.render(template, data)

^ Edge documentation

1 Like

Hello,
After changing that, I still have a “500” error.
My controller is next:

'use strict'
const Database = use('Database')

class GettingStartedController {
    index ({ request, response, view, antl }) {
        const navigations = await Database.from('navigation').first()
        return view.render('getting-start/index', {
            locales: antl.availableLocales(),
            navigations: navigations.toJSON()
        });
  }
}

module.exports = GettingStartedController

What’s the error output?

When I put

'use strict'
class WelcomeController {
    index ({ request, response, view, antl }) {
        return view.render('welcome', {
            locales: antl.availableLocales()
		});
  }
}

module.exports = WelcomeController

I have no more errors 500. This is another page for me to check if I have a problem or not.


I tested your code and I have this following error :

navigations.toJSON is not a function

The method does not exist. Query results are always in JSON. Just remove .toJSON()

For me it’s working fine.

If that doesn’t solve the problem (show output):

console.info(navigations);

I have Error 500, give me your code for test please (controller & view)

I’ve modified the code a little bit to make it faster for me to test :

controller :

// I use user table 
const users = await Database.from("users"); // You need to remove .first() to use @each
console.info(users);
return view.render("getting-start/index", {
  locales: "fr", /*I removed antl.availableLocales() for testing (no local translations)*/
  navigations: users
});

View (views\getting-start\index.edge) :

@each(navigation in navigations)
  <li><a href="{{ navigation.username }}">{{ navigation.password }}</a></li>
@endeach

I still have a 500 error right now.

'use strict'
const Database = use('Database')
class WelcomeController {
    index ({ request, response, view, antl }) {
		const navigation = await Database.from("navigation"); // You need to remove .first() to use @each
		console.info(navigation);
        return view.render('welcome', {
            locales: antl.availableLocales(),
            navigations: navigation
        });
  }
}

module.exports = WelcomeController

Check it out:

  • The table is called “navigation” and not “navigations” (migrations put the table name in the plural form)
  • You do have the antl installed

If that doesn’t solve the problem, put a try/catch :

...
  index({ request, response, view, antl }) {
    try {
      //all code
      });
    } catch (error) {
      console.info(error.message)
    }
  }
...

And try to solve with error output

Antl is been installed for me, i don’t have problem for this.
And, I always have the same mistake (500)
My database: http://i.prntscr.com/kvDMDurJT4WPBaHhVUhAIw.png

It’s hard to help you without an error message. Can you share your project (dm)?

Yes, no problem, if you wish I have Discord to discuss more quickly.

Thanks to my own error system, I managed to get the error in question.

	Unexpected token const navigation = await Database.from("navigation")