Working with reactions | laravel-comments | Spatie

 SPATIE

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

spatie.be/open-source

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

 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/v1/introduction)
- [ Getting a license ](https://spatie.be/docs/laravel-comments/v1/getting-a-license)
- [ Requirements ](https://spatie.be/docs/laravel-comments/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-comments/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-comments/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-comments/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-comments/v1/about-us)

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

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

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

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

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-comments                                                                                                                                                                                                                                    `

Working with reactions
======================

###  On this page

1. [ Creating a reaction ](#content-creating-a-reaction)
2. [ Retrieving reactions ](#content-retrieving-reactions)
3. [ Deleting a reaction ](#content-deleting-a-reaction)

A reaction is very short response, often just an emoji, on a comment.

Creating a reaction
-----------------------------------------------------------------------------------------------------------------

To create a reaction, call the `reaction` method on the comment.

```
$post->comment('Everybody pulled their socks up');

$comment->react('😍');
```

Behind the scenes, a `Spatie\Comments\Reaction` will be store that is associated with the comment and the currently logged in user.

To create a reaction for another user, you can pass that user as a second argument.

```
$anotherUser = User::whereFirst('email', 'john@beatles.com');

$comment->react('😍', $anotherUser);
```

A single user may have multiple reactions on a comment, but each reaction will be unique.

```
$post->comment('Everybody pulled their socks up');

$comment
    ->react('😍')
    ->react('👍')
    ->react('👍') // will not be stored as 👍 was already added as a reaction by the current user;
```

Retrieving reactions
--------------------------------------------------------------------------------------------------------------------

You can retrieve all reactions by using the `reactions` relation on a comment.

```
$comment->reactions;
```

The reactions will be returned in a `Spatie\Comments\Models\Collections\ReactionCollection`. That collection has a `summary` method that will return a summary of all reactions.

```
$summary = $comment->reactions->summary() // returns a Illuminate\Support\Collection;
```

The `$summary` will contain an item per unique reaction. Each item in `$summary` has these keys:

- `reaction`: the reaction itself, e.g. `😍`
- `count`: the number of users that gave this reaction on the comment
- `commentator_reacted`: a boolean that indicates whatever the currently logged in user gave this reaction

You can pass a `User` model to the `summary` method. The `commentator_reacted` in the return collection will be `true` when the given user has given that particular reaction.

Deleting a reaction
-----------------------------------------------------------------------------------------------------------------

To delete a particular reaction on a comment, you can call `deleteReaction`.

```
$comment->deleteReaction('😍');
```

The code above will delete the existing `😍` reaction on that `$comment` for the logged in user. If that reaction did not exist, nothing will happen.

To delete a reaction for another user, pass that user as second argument to `deleteReaction`.

```
$comment->deleteReaction('😍', $anotherUser);
```
