I am trying to get all the posts associated with my category

I am trying to get all the list of the posts for a category when the category is visited.

Route.get('/category/:slug', 'MainCategoryController.index')

Controller

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

async index ({ params:{slug}, view }) {
      
      const categories = await Category.query().where('slug', slug).with('post').first()
      return view.render('category.news', {
        categories: categories.toJSON()
      })
  }

The Category Model

      return this.belongsTo('App/Models/Post')
    }

The Post Model

      return this.belongsTo('App/Models/Category')
    }

Database i have a categories table with id, name, slug and i have a posts table with id, category_id the category_id reference to the categories table

My view i have

{{ category.post.subject }}
@endeach

and i get this error

undefined
undefined
undefined
undefined
undefined

Did you try with a hasMany in Category for posts and a const categories = await Category.query().where('slug', slug).posts().first()? That will fetch many posts in the context of a single category, as what you are trying to do?

After doing what you said i got this error Category.query(...).where(...).posts is not a function

Have you changed the relationship in the category model?

posts(){
   return this.hasMany('App/Models/Post')
 }

My Category Model

posts(){
      return this.hasMany('App/Models/Post')
    }

My Post Model

category(){
      return this.belongsTo('App/Models/Category')
    }
async index ({ params:{slug}, view }) {

      

    const categories = await Category.query().where('slug', slug).posts().first()

      return view.render('category.news', {

        categories: categories

      })

  }

You must change .posts() to .with(‘posts’)

I have changed it to

async index ({ params:{slug}, view }) {
      
    const categories = await Category.query().where('slug', slug).with('posts').first()
      return view.render('category.news', {
        categories: categories.toJSON()
      })
  }

My view

@each(category in categories)
{{ category.posts.subject }}
@endeach

i still get this

undefined
undefined
undefined
undefined
undefined
undefined

Now i am getting

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Controller

 async index ({ params:{slug}, view }) {
      
    const category = await Category.query().where('slug', slug).with('posts').fetch()
      return view.render('category.news', {
        category: category.toJSON()
      })
  }

Post Model

category(){
      return this.belongsTo('App/Models/Category')
    }

Category Model

posts(){
      return this.hasMany('App/Models/Post')
    }
@each(category in category)
{{ category.posts }}
@endeach

posts is an object. Object turns into [object Object] by default when .toString() is called on it.

You need to serialize it yourself or take certain value out of it. Like category.posts.title for example