Working with comments | laravel-comments | Spatie

 SPATIE

laravel-comments
================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-comments](https://spatie.be/docs/laravel-comments/v2)  Basic-usage  Working with comments

 Version   v2   v1

 Other versions for crawler [v2](https://spatie.be/docs/laravel-comments/v2) [v1](https://spatie.be/docs/laravel-comments/v1)

- [ Introduction ](https://spatie.be/docs/laravel-comments/v2/introduction)
- [ Getting a license ](https://spatie.be/docs/laravel-comments/v2/getting-a-license)
- [ Requirements ](https://spatie.be/docs/laravel-comments/v2/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-comments/v2/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-comments/v2/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-comments/v2/changelog)
- [ Upgrade guide ](https://spatie.be/docs/laravel-comments/v2/upgrade)
- [ About us ](https://spatie.be/docs/laravel-comments/v2/about-us)

Basic usage
-----------

- [ Working with comments ](https://spatie.be/docs/laravel-comments/v2/basic-usage/working-with-comments)
- [ Working with reactions ](https://spatie.be/docs/laravel-comments/v2/basic-usage/working-with-reactions)
- [ Transforming comments ](https://spatie.be/docs/laravel-comments/v2/basic-usage/transforming-comments)
- [ Sending notifications ](https://spatie.be/docs/laravel-comments/v2/basic-usage/sending-notifications)
- [ Approving comments ](https://spatie.be/docs/laravel-comments/v2/basic-usage/approving-comments)
- [ Listing comments in Laravel Nova ](https://spatie.be/docs/laravel-comments/v2/basic-usage/listing-comments-in-laravel-nova)

Livewire components
-------------------

- [ Introduction ](https://spatie.be/docs/laravel-comments/v2/livewire-components/introduction)
- [ Installation ](https://spatie.be/docs/laravel-comments/v2/livewire-components/installation)
- [ Using the components ](https://spatie.be/docs/laravel-comments/v2/livewire-components/using-the-components)
- [ Taking care of authorization ](https://spatie.be/docs/laravel-comments/v2/livewire-components/taking-care-of-authorization)
- [ Customising the views ](https://spatie.be/docs/laravel-comments/v2/livewire-components/customising-the-views)
- [ Mentions ](https://spatie.be/docs/laravel-comments/v2/livewire-components/mentions)
- [ Miscellaneous options ](https://spatie.be/docs/laravel-comments/v2/livewire-components/miscellaneous-options)
- [ Using Markdown ](https://spatie.be/docs/laravel-comments/v2/livewire-components/using-markdown)

 Working with comments
=====================

###  On this page

1. [ Creating a comment ](#content-creating-a-comment)
2. [ Handling markdown and other formats ](#content-handling-markdown-and-other-formats)
3. [ Getting all comments of a model ](#content-getting-all-comments-of-a-model)
4. [ Updating a comment ](#content-updating-a-comment)
5. [ Deleting a comment ](#content-deleting-a-comment)
6. [ Determining who made the comment ](#content-determining-who-made-the-comment)

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.

```
use Illuminate\Database\Eloquent\Model;
use Spatie\Comments\Models\Concerns\HasComments;

class Post extends Model
{
    use HasComments;
}
```

Creating a comment
--------------------------------------------------------------------------------------------------------------

To create a new comment, you can call the `comment` method.

```
$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', 'paul@beatles.com');

$post->comment("I've got a feeling", $anotherUser);
```

Because the `Comment` model itself uses the `HasComments` trait, you can create a nested comment like by calling `comment` on a `Comment`.

```
$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](https://spatie.be/docs/v1/laravel-comments/basic-usage/tranforming-comments).

Getting all comments of a model
-----------------------------------------------------------------------------------------------------------------------------------------------------

To get all comments of a model, use the `comments` relationship.

```
$allComments = $post->comments; // returns a relationship with all comments
```

You can get all unnested comments like this:

```
$unnestedComments =  $post->comments()->topLevel()->get();
```

Updating a comment
--------------------------------------------------------------------------------------------------------------

To update the content of a comment, update the `orginal_property` attribute of a comments model.

```
$comment->update([
    'original_text' => 'Updated comment',
]);
```

Deleting a comment
--------------------------------------------------------------------------------------------------------------

Simply call `delete` on a `Comment` model.

```
$comment->delete();
```

Determining who made the comment
--------------------------------------------------------------------------------------------------------------------------------------------------------

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 `null`.

You can use these methods to determine why there was no commentator found.

```
$comment->wasMadeByDeletedCommentator(); // returns a boolean
$comment->wasMadeAnonymously(); // returns a boolean
```
