Reputation: 308
Ok so the basis is that I want to be able to have comments on each of my post. The comments will have a reply button for each. It’s not multi threaded so one comment will have many replies. Those replies won’t have replies. Also I want to be able to like and dislike a comment/reply. All of this will not be bind to the user model or any of such. The public visitors will be able to add comments and reply to comments but the approval is needed.
So here is the logic I got so far. Am I on the right track here (hoping this post may help someone else as well):
So i create aComment
model. And then create a table named comments
.
And I create a model named Reply
and a table named replies
And finally, a model name Like
and it’s table likes
So the relationship is:
comments
will have many replies
and replies
belongs to one comment
replies
& comments
will have many likes
.
And now for the logic:
I will use AJAX to call the store function on the CommentController
to store comments. And I will call the store function on the ReplyController
to store the replies. As for the likes, LikeController
store function will store the likes for the comment and reply.
Here is the table structure:
Comments table
Replies table
Likes table
Now what I do not understand is, likes table. Is it right to have comment_id
and reply_id
and also like
and dislike
?
I could call the store function everytime someone clicks the like or dislike and have it stored in the table and update the column if it is a reply or a comment by it’s id
to the respective id columns. Is the logic right?
Also, if you guys have any suggestions or better and efficient way of doing this, please let me know. This is getting too long so I’ll just leave it here.
Edit
Also forgot to mention that I am not sure how I will be taking the amount of likes from db to blade and count it. Not with the current structure mentioned above. Also, how to check and see if the person already liked. And if so, don’t let them like again or dislike again. A liked person can not dislike. They can do only one thing.
Upvotes: 1
Views: 575
Reputation: 304
I don't see any need of the Replies Table just use parent_id in comments table. This tutorial might help you get started Nesting Comments in Laravel
Upvotes: 1
Reputation: 35190
I would definitely recommend associating comments, replied and likes to a User
which you are pretty much already doing with comments and replies.
There are ways that you could have a "liking" system that would allow guest usage, however, this would be very easy to get around and ultimately make your "like" stats useless.
An example DB structure going forward would be:
comments and replies
likes
The likes
table is setup to be used as a Polymorphic relationship e.g. in your Comment
model:
public function likes()
{
return $this->morphMany(Like::class, 'likeable');
}
Then to add a like you would have something like:
$like = new Like(['user_id' => auth()->user()->id, 'liked' => true]);
$comment->likes()->save();
There are many different ways you would then check if the current auth user has liked a post, one example would be to have a separate relationship so that you can eager load the results:
public function authUserLike()
{
return $this->morphOne(Like::class, 'likeable')->where('user_id', auth()->id());
}
Then if the auth_user_like
is null
they haven't already liked that comment.
Upvotes: 1