Hello,
Is it possible to create eloquent relations for the following situation:
A blogs page where blogs have comments and the comments have likes as well as replies.
How would I have to create my relations?
Thanks!
Hi! This is what I'll do, I'll create three models. Blog, Post (I like to call it as post instead of page) and Comment. Then I'll create one-to-many relationships.
Blog one-to-many Post
Post one-to-many Comment
In your comments table you could just add likes and reply_id field.
I hope this helps.
sherwinncnadev said:
Hi! This is what I'll do, I'll create three models. Blog, Post (I like to call it as post instead of page) and Comment. Then I'll create one-to-many relationships.
Blog one-to-many Post
Post one-to-many CommentIn your comments table you could just add likes and reply_id field.
I hope this helps.
Hello, thank you for your reply.
This is how I actually implemented it and it works perfectly. However, if I want to format the comments in the HTML, I need to have it chunked by replies. Because, when a comment is a reply to another comment, the reply should be displayed under the "main" comment. How would you be doing this?
I hope this makes sense.
The first solution that popped in my head is you could create a recursive function that accepts an eloquent collection of comments and what it'll do is loop through it all and check if a comment has a reply. If it has a reply call the function again. I suggest you should do a paging for comments since this approach will slow your website if it's handling a lot of comments.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community