Mentions | laravel-comments | Spatie

 SPATIE

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-comments](https://spatie.be/docs/laravel-comments/v2)  Livewire-components  Mentions

 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)

 Mentions
========

###  On this page

1. [ Customising the autocomplete ](#content-customising-the-autocomplete)
2. [ Rendering mentions ](#content-rendering-mentions)
3. [ Custom rendering of mentions ](#content-custom-rendering-of-mentions)
4. [ Updating usernames ](#content-updating-usernames)
5. [ Mentions with a custom editor ](#content-mentions-with-a-custom-editor)
6. [ Mentions and anonymous comments ](#content-mentions-and-anonymous-comments)

The Livewire component gives you the option to enable mentions in your comments.

If mentions are enabled your users will be able to start searching for a user they want to mention by typing `@` in the comment field.

![](/docs/laravel-comments/v2/images/mention.png)When a comment with a mention is created users will receive a notification of the created comment.

Out of the box mentions will only autocomplete when using the EasyMDE editor.

Customising the autocomplete
--------------------------------------------------------------------------------------------------------------------------------------------

By default the autocomplete will do a search on the configured name field for you commentator model.

You can customise this by defining your own Resolver class and adding it to the `actions.resolve_mentions_autocomplete` key in the `comments.php` config file.

For example if you want to add a custom scope to the search you could do the following:

```
class CustomResolveMentionsAutocompleteAction extends ResolveMentionsAutocompleteAction
{
    public function resolve(string $query, $commentable): array
    {
        $modelClass = Config::commentator();

        return $modelClass::where(Config::nameField(), 'like', "%$query%")
            ->notBanned()
            ->limit(10)
            ->get()
            ->toArray();
    }
}
```

Rendering mentions
--------------------------------------------------------------------------------------------------------------

By default we recommend to render the mentions by adding the `MentionsTransformer` to the `comment_transformers` array in the `comments.php` config file.

Custom rendering of mentions
--------------------------------------------------------------------------------------------------------------------------------------------

If you want to customise how these mentions are rendered you can extend and the `MentionsTransformer` and add your own logic.

```
class CustomMentionsTransformer implements MentionsTransformer
{
    public function renderMention(CanComment $mentionee): string
    {
        return $mentionee->commentatorProperties()->name;
    }
}
```

Or render a custom Blade component:

```
class CustomMentionsTransformer implements MentionsTransformer
{
    public function renderMention(CanComment $mentionee): string
    {
        return Blade::renderComponent(new CustomMentionComponent($mentionee));
    }
}
```

Updating usernames
--------------------------------------------------------------------------------------------------------------

Because comments are stored as HTML in the database when a user changes his username this will not reflect in the comments. However on each save of a comment the configure transformers will be called to render the comment and the username will be updated.

We recommend rolling your own system to suit your needs.

An example of how to do this is dispatching a Job when a user changes his name. This could look something like:

```
class UpdateUsernameInCommentsJob implements ShouldQueue
{
    use Queueable;

    public function __construct(protected User $user)
    {
    }

    public function handle(): void
    {
        $comments = Comment::whereLike('original_text', "%data-mention=\"{$this->user->id}\"%")->get();

        $comments->each(function($comment) {
            $comment->save();
        });
    }
}
```

Mentions with a custom editor
-----------------------------------------------------------------------------------------------------------------------------------------------

If you want to use mentions with your own editor you could implement your own system for this. To be compatible with this package the only thing you need to make sure is that you store the mentions in the following format:

```
## This is the raw comment

John Doe is a great guy!
```

The `data-mentionee` attribute needs to be equal to the id of the user/commentator you are mentioning.

Mentions and anonymous comments
-----------------------------------------------------------------------------------------------------------------------------------------------------

Mentions will work when anonymous comments are enabled, but you need to make sure the `comments.models.commentator` config key is set.
