├── .editorconfig ├── CHANGELOG.md ├── CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE.md ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── composer.json ├── phpunit.xml └── src ├── Contracts └── ReactableInterface.php ├── Migrations ├── 2017_03_25_141250_create_reactions_table.php └── 2017_03_25_142853_create_reactables_table.php ├── Models └── Reaction.php ├── Providers └── ReactionsServiceProvider.php └── Traits ├── Reactable.php └── Reacts.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `laravel-reactions` will be documented in this file. 4 | 5 | Updates are following the [Keep a CHANGELOG](http://keepachangelog.com/) principles. 6 | 7 | ## 1.1.0 - 2024-06-26 8 | 9 | ### Added 10 | 11 | - Add support of Laravel 11; 12 | 13 | ## 1.1.0 - 2023-10-19 14 | 15 | ### Added 16 | 17 | - 18 | - Add support of Laravel 10; 19 | - 20 | ## 0.1.1 - 2017-03-26 21 | 22 | ### Added 23 | - `Reaction` model that represents a reaction; 24 | - `Reactable`, `Reacts` and `ReactlableInterface` to add reaction features to models; 25 | - migrations for `reactions` and `reactables` tables; 26 | - service provider to execute migrations easily; 27 | 28 | ### Deprecated 29 | - Nothing 30 | 31 | ### Fixed 32 | - Nothing 33 | 34 | ### Removed 35 | - Nothing 36 | 37 | ### Security 38 | - Nothing 39 | -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to make participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at `hellofrancesco@gmail.com`. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/thedevdojo/laravel-reactions). 6 | 7 | 8 | ## Pull Requests 9 | 10 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - Check the code style with ``$ composer check-style`` and fix it with ``$ composer fix-style``. 11 | 12 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 13 | 14 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | - **Create feature branches** - Don't ask us to pull from your master branch. 19 | 20 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 21 | 22 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 23 | 24 | 25 | ## Running Tests 26 | 27 | ``` bash 28 | $ composer test 29 | ``` 30 | 31 | 32 | **Happy coding**! 33 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Detailed description 4 | 5 | Provide a detailed description of the change or addition you are proposing. 6 | 7 | Make it clear if the issue is a bug, an enhancement or just a question. 8 | 9 | ## Context 10 | 11 | Why is this change important to you? How would you use it? 12 | 13 | How can it benefit other users? 14 | 15 | ## Possible implementation 16 | 17 | Not obligatory, but suggest an idea for implementing addition or change. 18 | 19 | ## Your environment 20 | 21 | Include as many relevant details about the environment you experienced the bug in and how to reproduce it. 22 | 23 | * Version used (e.g. PHP 5.6, HHVM 3): 24 | * Operating system and version (e.g. Ubuntu 16.04, Windows 7): 25 | * Link to your project: 26 | * ... 27 | * ... 28 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Francesco Malatesta 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Description 4 | 5 | Describe your changes in detail. 6 | 7 | ## Motivation and context 8 | 9 | Why is this change required? What problem does it solve? 10 | 11 | If it fixes an open issue, please link to the issue here (if you write `fixes #num` 12 | or `closes #num`, the issue will be automatically closed when the pull is accepted.) 13 | 14 | ## How has this been tested? 15 | 16 | Please describe in detail how you tested your changes. 17 | 18 | Include details of your testing environment, and the tests you ran to 19 | see how your change affects other areas of the code, etc. 20 | 21 | ## Screenshots (if appropriate) 22 | 23 | ## Types of changes 24 | 25 | What types of changes does your code introduce? Put an `x` in all the boxes that apply: 26 | - [ ] Bug fix (non-breaking change which fixes an issue) 27 | - [ ] New feature (non-breaking change which adds functionality) 28 | - [ ] Breaking change (fix or feature that would cause existing functionality to change) 29 | 30 | ## Checklist: 31 | 32 | Go over all the following points, and put an `x` in all the boxes that apply. 33 | 34 | Please, please, please, don't send your pull request until all of the boxes are ticked. Once your pull request is created, it will trigger a build on our [continuous integration](http://www.phptherightway.com/#continuous-integration) server to make sure your [tests and code style pass](https://help.github.com/articles/about-required-status-checks/). 35 | 36 | - [ ] I have read the **[CONTRIBUTING](CONTRIBUTING.md)** document. 37 | - [ ] My pull request addresses exactly one patch/feature. 38 | - [ ] I have created a branch for this patch/feature. 39 | - [ ] Each individual commit in the pull request is meaningful. 40 | - [ ] I have added tests to cover my changes. 41 | - [ ] If my change requires a change to the documentation, I have updated it accordingly. 42 | 43 | If you're unsure about any of these, don't hesitate to ask. We're here to help! 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Reactions 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Build Status][ico-travis]][link-travis] 6 | [![Quality Score][ico-code-quality]][link-code-quality] 7 | [![Total Downloads][ico-downloads]][link-downloads] 8 | 9 | Laravel Reactions is the package you need if you want to implement reactions for your Eloquent models, in a similar way you can see on Facebook. 10 | 11 | ![](https://cdn.devdojo.com/open-source/reactions.jpg) 12 | 13 | #### Features 14 | 15 | * easy to install, nothing to configure; 16 | * ready-to-use traits; 17 | * you can implement reactions for multiple entities, thanks to a polymorphic many to many relationship; 18 | * you can implement reactions from multiple entities, thanks to some extra magic under the hood; 19 | 20 | #### Disclaimer 21 | 22 | **This project is not associated with Facebook in any way.** I've used the "reactions" just to give an idea of the concept. In case of legal issues, let me know using an email. 23 | 24 | ## Install 25 | 26 | Install the package with Composer. 27 | 28 | ``` bash 29 | $ composer require DevDojo/laravel-reactions 30 | ``` 31 | 32 | Add the Service Provider to your `config/app.php` file. 33 | 34 | ```php 35 | DevDojo\LaravelReactions\Providers\ReactionsServiceProvider::class, 36 | ``` 37 | 38 | Run the migrations to create `reactions` and `reactables` tables. 39 | 40 | ```bash 41 | $ php artisan migrate 42 | ``` 43 | 44 | You're good to go. 45 | 46 | ## Usage 47 | 48 | ### Add Traits to Models 49 | 50 | To use the package you need to follow two steps: 51 | 52 | * add the `DevDojo\LaravelReactions\Traits\Reacts` trait to the entity that is going to react to something; 53 | * add the `DevDojo\LaravelReactions\Traits\Reactable` trait to the entity that is going to "receive" reactions; 54 | * be sure that the entity that receives reactions also implements the `DevDojo\LaravelReactions\Contracts\ReactableInterface`; 55 | 56 | Let's make an example. 57 | 58 | Imagine that you have some users in your application. You are building a blog, so you will have posts. 59 | 60 | You want to let your user add reactions to your posts. Just like Facebook, you know. 61 | 62 | Let's say we have two models: `User` and `Post`. 63 | 64 | Following the steps, we first add the `DevDojo\LaravelReactions\Traits\Reacts` trait to our `User` model. 65 | 66 | ```php 67 | use DevDojo\LaravelReactions\Traits\Reacts; 68 | 69 | class User extends Model { 70 | use Reacts; 71 | } 72 | ``` 73 | 74 | Done! Now, to the `Post` model! 75 | 76 | ```php 77 | use DevDojo\LaravelReactions\Traits\Reactable; 78 | use DevDojo\LaravelReactions\Contracts\ReactableInterface; 79 | 80 | class Post extends Model implements ReactableInterface { 81 | use Reactable; 82 | } 83 | ``` 84 | 85 | Ta-dah! You're done. 86 | 87 | By default, the package ships with a `Reaction` model. This model has a single, simple property: its `name`. You can create a new one easily, with 88 | 89 | ```php 90 | $likeReaction = Reaction::createFromName('like'); 91 | $likeReaction->save(); 92 | 93 | $loveReaction = Reaction::createFromName('love'); 94 | $loveReaction->save(); 95 | ``` 96 | 97 | ### React! 98 | 99 | Our models are ready. We can use them. How? 100 | 101 | ```php 102 | // picking the first user, for this example... 103 | $user = User::first(); 104 | 105 | // the previously created reaction 106 | $likeReaction = Reaction::where('name', '=', 'like')->first(); 107 | 108 | // picking up a post... 109 | $awesomePost = Post::first(); 110 | 111 | // react to it! 112 | $user->reactTo($awesomePost, $likeReaction); 113 | ``` 114 | 115 | Easy, isn't it? The `reactTo` method handles everything for you. 116 | 117 | ### Get Reactions for a Model 118 | 119 | Just like you can let one of your entities react to another one, you should be able to get all the reactions for an entity. 120 | 121 | Let's see how to do it. 122 | 123 | ```php 124 | // picking up a post... 125 | $awesomePost = Post::first(); 126 | 127 | // get all reactions 128 | $reactions = $awesomePost->reactions; 129 | ``` 130 | 131 | In `$reactions` you will have a collection of `Reaction` models, ready to be used. 132 | 133 | ### Get a Reactions Summary 134 | 135 | Probably you won't need everything about reactions to a specific entity everytime. So, I implemented a `getReactionsSummary` for you. 136 | 137 | ```php 138 | // picking up a post... 139 | $awesomePost = Post::first(); 140 | 141 | // get a summary of related reactions 142 | $reactionsSummary = $awesomePost->getReactionsSummary(); 143 | ``` 144 | 145 | In `$reactionsSummary` you will find a collection of items, composed by two properties: `name` and `count`. Imagine that we do something like the following code in a controller: 146 | 147 | ```php 148 | $reactionsSummary = $awesomePost->getReactionsSummary(); 149 | return $reactionsSummary; 150 | ``` 151 | 152 | Here's what we will get: 153 | 154 | ```json 155 | [ 156 | { 157 | "name": "like", 158 | "count": 12 159 | }, 160 | { 161 | "name": "love", 162 | "count": 7 163 | } 164 | ] 165 | ``` 166 | 167 | ### Accessing the "Responder" 168 | 169 | When on Facebook, you can see "who" reacted in some way to a post. To get that `who` you can use the `getResponder` method. This works for every reaction you get using the `reactions` relationship method, of course. 170 | 171 | Let's assume that a `User` named "Francesco" already reacted with the "love" reaction to a post. 172 | 173 | ```php 174 | // our awesome post. 175 | $awesomePost = Post::first(); 176 | 177 | // every $reaction is a Reaction model 178 | foreach($awesomePost->reactions as $reaction) 179 | { 180 | $user = $reaction->getResponder(); 181 | 182 | // this will output "Francesco" 183 | echo $user->name; 184 | } 185 | ``` 186 | 187 | ## Change log 188 | 189 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 190 | 191 | ## Testing 192 | 193 | ``` bash 194 | $ vendor/bin/phpunit 195 | ``` 196 | 197 | ## Contributing 198 | 199 | Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. 200 | 201 | ## Security 202 | 203 | If you discover any security related issues, please email hellofrancesco@gmail.com instead of using the issue tracker. 204 | 205 | ## Credits 206 | 207 | - [Francesco Malatesta][link-author] 208 | - [All Contributors][link-contributors] 209 | 210 | ## License 211 | 212 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 213 | 214 | [ico-version]: https://img.shields.io/packagist/v/DevDojo/laravel-reactions.svg?style=flat-square 215 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 216 | [ico-travis]: https://img.shields.io/travis/DevDojo/laravel-reactions/master.svg?style=flat-square 217 | [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/DevDojo/laravel-reactions.svg?style=flat-square 218 | [ico-code-quality]: https://img.shields.io/scrutinizer/g/DevDojo/laravel-reactions.svg?style=flat-square 219 | [ico-downloads]: https://img.shields.io/packagist/dt/DevDojo/laravel-reactions.svg?style=flat-square 220 | 221 | [link-packagist]: https://packagist.org/packages/DevDojo/laravel-reactions 222 | [link-travis]: https://travis-ci.org/DevDojo/laravel-reactions 223 | [link-scrutinizer]: https://scrutinizer-ci.com/g/DevDojo/laravel-reactions/code-structure 224 | [link-code-quality]: https://scrutinizer-ci.com/g/DevDojo/laravel-reactions 225 | [link-downloads]: https://packagist.org/packages/DevDojo/laravel-reactions 226 | [link-author]: https://github.com/DevDojo 227 | [link-contributors]: ../../contributors 228 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "devdojo/laravel-reactions", 3 | "type": "library", 4 | "description": "", 5 | "keywords": [ 6 | "laravel", 7 | "package", 8 | "reactions", 9 | "like", 10 | "love", 11 | "eloquent", 12 | "trait" 13 | ], 14 | "homepage": "https://github.com/thedevdojo/laravel-reactions", 15 | "license": "MIT", 16 | "authors": [ 17 | { 18 | "name": "Tony Lea", 19 | "email": "tony@devdojo.com", 20 | "homepage": "https://devdojo.com", 21 | "role": "Developer" 22 | } 23 | ], 24 | "require": { 25 | "php" : "^8.1|^8.2", 26 | "illuminate/database": "^9.0|^10.0|^11.0", 27 | "illuminate/support": "^9.0|^10.0|^11.0" 28 | }, 29 | "require-dev": { 30 | "friendsofphp/php-cs-fixer": "^3.35", 31 | "mockery/mockery": "^1.6", 32 | "orchestra/testbench": "^8.0", 33 | "phpunit/phpunit": "^9.5.0" 34 | }, 35 | "autoload": { 36 | "psr-4": { 37 | "DevDojo\\LaravelReactions\\": "src" 38 | } 39 | }, 40 | "autoload-dev": { 41 | "psr-4": { 42 | "DevDojo\\LaravelReactions\\Tests\\": "tests" 43 | } 44 | }, 45 | "scripts": { 46 | "test": "phpunit", 47 | "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", 48 | "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" 49 | }, 50 | "extra": { 51 | "branch-alias": { 52 | "dev-master": "1.0-dev" 53 | } 54 | }, 55 | "config": { 56 | "sort-packages": true 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | src/ 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | tests/Unit 16 | 17 | 18 | tests/Integration 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/Contracts/ReactableInterface.php: -------------------------------------------------------------------------------- 1 | getReactionsSummary(); 18 | * foreach($summaryItems as $reaction) { 19 | * // gets the reaction name 20 | * $reaction->name 21 | * 22 | * // gets the given reactions count for the $post 23 | * $reaction->count 24 | * } 25 | * 26 | */ 27 | public function getReactionsSummary(); 28 | } 29 | -------------------------------------------------------------------------------- /src/Migrations/2017_03_25_141250_create_reactions_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->uuid()->nullable(); 19 | 20 | $table->string('name'); 21 | 22 | $table->timestamps(); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::drop('reactions'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Migrations/2017_03_25_142853_create_reactables_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | 19 | $table->unsignedInteger('reaction_id'); 20 | 21 | $table->morphs('reactable'); 22 | $table->morphs('responder'); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::drop('reactables'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Models/Reaction.php: -------------------------------------------------------------------------------- 1 | name = $name; 13 | 14 | return $reaction; 15 | } 16 | 17 | public function getResponder() 18 | { 19 | if ($this->getOriginal('pivot_responder_type', null)) { 20 | return forward_static_call( 21 | [$this->getOriginal('pivot_responder_type'), 'find'], 22 | $this->getOriginal('pivot_responder_id') 23 | ); 24 | } 25 | 26 | return null; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Providers/ReactionsServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadMigrationsFrom(__DIR__.'/../Migrations'); 17 | } 18 | 19 | /** 20 | * Register the application services. 21 | * 22 | * @return void 23 | */ 24 | public function register() 25 | { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Traits/Reactable.php: -------------------------------------------------------------------------------- 1 | morphToMany('DevDojo\\LaravelReactions\\Models\\Reaction', 'reactable') 15 | ->withPivot(['responder_id', 'responder_type']); 16 | } 17 | 18 | /** 19 | * @return \Illuminate\Database\Eloquent\Collection|static[] 20 | */ 21 | public function getReactionsSummary() 22 | { 23 | return $this->reactions() 24 | ->getQuery() 25 | ->select('name', \DB::raw('count(*) as count')) 26 | ->groupBy('name') 27 | ->get(); 28 | } 29 | 30 | public function reacted($responder = null) 31 | { 32 | if (is_null($responder)) { 33 | $responder = auth()->user(); 34 | } 35 | 36 | return $this->reactions() 37 | ->where('responder_id', $responder->id) 38 | ->where('responder_type', get_class($responder))->exists(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Traits/Reacts.php: -------------------------------------------------------------------------------- 1 | reactions() 13 | ->where('responder_id', $this->getKey()) 14 | ->where('responder_type', get_class($this))->first(); 15 | 16 | 17 | $currentReactedName = ''; 18 | if($reactedToReaction){ 19 | $currentReactedName = $reactedToReaction->name; 20 | $this->deleteReaction($reactable, $reactedToReaction); 21 | 22 | } 23 | 24 | $reacted = $reactable->reactions()->where([ 25 | 'responder_id' => $this->getKey() 26 | ])->first(); 27 | 28 | if ( !$reacted && ($currentReactedName != $reaction->name) ) { 29 | return $this->storeReaction($reactable, $reaction); 30 | } 31 | 32 | return null; 33 | 34 | } 35 | 36 | public function hasReaction(ReactableInterface $reactable){ 37 | // return $this->reactions() 38 | // ->where('responder_id', $this->id) 39 | // ->where('responder_type', get_class($this)) 40 | // ->where('')->exists(); 41 | return $reactable->reacted(); 42 | } 43 | 44 | /** 45 | * Store reaction. 46 | * 47 | * @param ReactableInterface $reactable 48 | * @param mixed $type 49 | * @return \Qirolab\Laravel\Reactions\Models\Reaction 50 | */ 51 | protected function storeReaction(ReactableInterface $reactable, Reaction $reaction) 52 | { 53 | $reactable->reactions()->attach( 54 | $reaction->id, 55 | [ 56 | 'responder_id' => $this->getKey(), 57 | 'responder_type' => get_class($this) 58 | ] 59 | ); 60 | 61 | // $reaction = $reactable->reactions()->create([ 62 | // 'user_id' => $this->getKey(), 63 | // 'type' => $type, 64 | // ]); 65 | //event(new OnReaction($reactable, $reaction, $this)); 66 | return $reaction; 67 | } 68 | 69 | protected function deleteReaction(ReactableInterface $reactable, Reaction $reacted) 70 | { 71 | $reactable->reactions()->wherePivot('reaction_id', $reacted->id)->wherePivot('responder_id', $this->id)->wherePivot('responder_type', get_class($this))->detach(); 72 | } 73 | } 74 | --------------------------------------------------------------------------------