Comments will be always be associated with Eloquent models. To allow an Eloquent model to have comments associated with it, use the
Spatie\Comments\HasComments trait on it.
class Post extends Model
To create a new comment, you can call the
$post->comment("I've got a feeling");
Behind the scene new
Spatie\Comments\Comment model will be saved. The comment will be associated with the
$post and the currently logged in user.
You can associate the comment with another user, by passing it as a second argument.
$anotherUser = User::whereFirst('email', 'firstname.lastname@example.org');
$post->comment("I've got a feeling", $anotherUser);
Comment model itself uses the
HasComments trait, you can create a nested comment like by calling
comment on a
$comment = $post->comment("I've got a feeling");
$nestedComment = $comment->comment("It keeps me on my toes")
##Handling markdown and other formats
To add support for markdown, take a look at the section on transforming comments.
To get all comments of a model, use the
$allComments = $post->comments; // returns a relationship with all comments
You can get all unnested comments like this:
$unnestedComments = $post->comments()->topLevel()->get();
To update the content of a comment, update the
orginal_property attribute of a comments model.
'original_text' => 'Updated comment',
delete on a
You can use the
commentator relationship to determine who made a comment.
$user = $comment->commentator
If the user that made the comment was deleted, or that comment was made anonymously, then
$comment->commentator will return
You can use these methods to determine why there was no commentator found.
$comment->wasMadeByDeletedCommentator(); // returns a boolean
$comment->wasMadeAnonymously(); // returns a boolean