1

I have a forum with a lot of topics (written in Angular+Laravel+Xampp), and I wondered how I should display the last comment when the user don't open specific topic, only seeing the headers/titles.

My structure is: There are sections (categories), and a section can have many topics, and a topic can have many posts, and a post can have many comments.

When a user views all the sections (the biggest categories), I wanna display for each of them the last messages in those topics'.

I could just search for every post and see which one of them has the latest comment, but then I should search through all the comments which seems a little redundant.

Maybe I should make a column e.g. "last_comment" for the sections table, and update it every time a post get a comment?

What is the best approach?

  • 1
    Does [this](https://stackoverflow.com/questions/43600674/laravel-eloquent-get-the-latest-row-of-related-table#answer-43600811) answer your question? – Tpojka Apr 30 '20 at 09:24
  • Kind of, I knew I could do this way but I was wondering if there was a preferred way so I wouldn't use something not considered "good habit". Anyway, thanks for the suggestion! – Giuseppe The Dreamer Apr 30 '20 at 12:28
  • Separated relation or maybe [scope](https://laravel.com/docs/7.x/eloquent#query-scopes) are your options I'd say. – Tpojka Apr 30 '20 at 13:03

0 Answers0