├── .php-cs-fixer.cache ├── .php_cs.dist.php ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── composer.json ├── migrations └── 2021_07_11_000000_create_reports_table.php └── src ├── Contracts ├── ReportableInterface.php └── ReportsInterface.php ├── Events ├── OnDeleteReport.php └── OnReport.php ├── Exceptions └── InvalidReportUser.php ├── Models └── Report.php ├── ReportsServiceProvider.php └── Traits ├── Reportable.php └── Reports.php /.php-cs-fixer.cache: -------------------------------------------------------------------------------- 1 | {"php":"8.1.2","version":"3.6.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"class_definition":{"space_before_parenthesis":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Exceptions\/InvalidReactionUser.php":585702289,"src\/Models\/Reaction.php":1279343624,"src\/Events\/OnDeleteReaction.php":2828042155,"src\/Events\/OnReaction.php":3552881746,"src\/Traits\/Reacts.php":2435420820,"src\/Traits\/Reactable.php":3024489155,"src\/ReactionsServiceProvider.php":4061742279,"src\/Contracts\/ReactableInterface.php":3475897493,"src\/Contracts\/ReactsInterface.php":2321031172,"tests\/Unit\/ReactsReactionEventTest.php":2540233616,"tests\/Unit\/ReactionModelTest.php":2829956474,"tests\/Unit\/ReactableTest.php":706048754,"tests\/Unit\/ReactsModelTest.php":813193790,"tests\/Unit\/ReactableScopeTest.php":3403419430,"tests\/Unit\/ReactableReactionEventTest.php":350782567,"tests\/Unit\/InvalidReactionUserExceptionTest.php":101659214,"tests\/TestCase.php":2398752019,"tests\/database\/migrations\/2018_07_11_000000_create_articles_table.php":1591154406,"tests\/database\/migrations\/2018_07_10_000000_create_users_table.php":436823156,"tests\/Stubs\/Models\/User.php":393950944,"tests\/Stubs\/Models\/Article.php":2470098451}} -------------------------------------------------------------------------------- /.php_cs.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__.'/src', 6 | __DIR__.'/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **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](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Reza Ghasemzadeh 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Reports 2 | ![laravel-reports](https://user-images.githubusercontent.com/36597017/200645468-274bc278-f0fb-4430-a2f1-ebae4a6fa986.jpg) 3 | 4 | Laravel reports package for implementing reports (eg: Spam,Violence,Child Abuse,illegal Drugs, etc.) on Eloquent 5 | models. 6 | 7 | ## Installation 8 | 9 | Download package into the project using Composer. 10 | 11 | ```bash 12 | $ composer require rezaghz/laravel-reports 13 | ``` 14 | 15 | ### Registering package 16 | 17 | > Laravel 5.5 (or higher) uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider. 18 | 19 | For Laravel 5.4 or earlier releases version include the service provider within `app/config/app.php`: 20 | 21 | ```php 22 | 'providers' => [ 23 | Rezaghz\Laravel\Reports\ReportsServiceProvider::class, 24 | ], 25 | ``` 26 | 27 | ### Database Migration 28 | 29 | If you want to make changes in migrations, publish them to your application first. 30 | 31 | ```bash 32 | $ php artisan vendor:publish --provider="Rezaghz\Laravel\Reports\ReportsServiceProvider" --tag=migrations 33 | ``` 34 | 35 | Run database migrations. 36 | 37 | ```bash 38 | $ php artisan migrate 39 | ``` 40 | 41 | ## Usage 42 | 43 | ### Prepare Reports (User) Model 44 | 45 | Use `Rezaghz\Laravel\Reports\Contracts\ReportsInterface` contract in model which will perform report behavior on 46 | reportable model and implement it and use `Rezaghz\Laravel\Reports\Traits\Reports` trait. 47 | 48 | ```php 49 | use Rezaghz\Laravel\Reports\Traits\Reports; 50 | use Rezaghz\Laravel\Reports\Contracts\ReportsInterface; 51 | use Illuminate\Foundation\Auth\User as Authenticatable; 52 | 53 | class User extends Authenticatable implements ReportsInterface 54 | { 55 | use Reports; 56 | } 57 | ``` 58 | 59 | ### Prepare Reportable Model 60 | 61 | Use `Rezaghz\Laravel\Reports\Contracts\ReportableInterface` contract in model which will get report behavior and 62 | implement it and use `Rezaghz\Laravel\Reports\Traits\Reportable` trait. 63 | 64 | ```php 65 | use Illuminate\Database\Eloquent\Model; 66 | use Rezaghz\Laravel\Reports\Traits\Reportable; 67 | use Rezaghz\Laravel\Reports\Contracts\ReportableInterface; 68 | 69 | class Article extends Model implements ReportableInterface 70 | { 71 | use Reportable; 72 | } 73 | ``` 74 | 75 | ## Available Methods 76 | 77 | ### Report 78 | 79 | ```php 80 | $user->reportTo($article, 'spam'); 81 | 82 | $article->report('spam'); // current login user 83 | $article->report('spam', $user); 84 | ``` 85 | 86 | ### Remove Report 87 | 88 | Removing report of user from reportable model. 89 | 90 | ```php 91 | $user->removeReportFrom($article); 92 | 93 | $article->removeReport(); // current login user 94 | $article->removeReport($user); 95 | ``` 96 | 97 | ### Toggle Report 98 | 99 | The toggle report method will add a report to the model if the user has not reported. If a user has already reported, 100 | then it will replace the previous report with a new report. For example, if the user has reported 'spam' on the model. 101 | Now on toggles report to 'violence' then it will remove the 'spam' and stores the 'violence' report. 102 | 103 | If a user has reported `spam` then on toggle report with `spam`. It will remove the report. 104 | 105 | ```php 106 | $user->toggleReportOn($article, 'spam'); 107 | 108 | $article->toggleReport('spam'); // current login user 109 | $article->toggleReport('spam', $user); 110 | ``` 111 | 112 | ### Boolean check if user reported on model 113 | 114 | ```php 115 | $user->isReportedOn($article)); 116 | 117 | $article->is_reported; // current login user 118 | $article->isReportBy(); // current login user 119 | $article->isReportBy($user); 120 | ``` 121 | 122 | ### Report summary on model 123 | 124 | ```php 125 | $article->reportSummary(); 126 | $article->report_summary; 127 | 128 | // example 129 | $article->report_summary->toArray(); 130 | // output 131 | /* 132 | [ 133 | "spam" => 5, 134 | "violence" => 2, 135 | "illegal_drugs" => 4, 136 | "child_abuse" => 1 137 | ] 138 | */ 139 | ``` 140 | 141 | ### Get collection of users who reported on model 142 | 143 | ```php 144 | $article->reportsBy(); 145 | ``` 146 | 147 | ### Scopes 148 | 149 | Find all articles reported by user. 150 | 151 | ```php 152 | Article::whereReportedBy()->get(); // current login user 153 | 154 | Article::whereReportedBy($user)->get(); 155 | Article::whereReportedBy($user->id)->get(); 156 | ``` 157 | 158 | ### Report on Model 159 | 160 | ```php 161 | // It will return the Report object that is reported by given user. 162 | $article->reported($user); 163 | $article->reported(); // current login user 164 | $article->reported; // current login user 165 | 166 | $user->reportedOn($article); 167 | ``` 168 | 169 | ### Events 170 | 171 | On each report added `\Rezaghz\Laravel\Reports\Events\OnReport` event is fired. 172 | 173 | On each report removed `\Rezaghz\Laravel\Reports\Events\OnDeleteReport` event is fired. 174 | 175 | ### Testing 176 | 177 | Run the tests with: 178 | 179 | ```bash 180 | $ vendor/bin/phpunit 181 | ``` 182 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rezaghz/laravel-reports", 3 | "description": "Laravel reports package for implementing reports (eg: Spam,Violence,Child Abuse,illegal Drugs etc) on Eloquent models.", 4 | "type": "library", 5 | "keywords": [ 6 | "laravel", 7 | "eloquent", 8 | "report", 9 | "like", 10 | "dislike", 11 | "unlike", 12 | "rate", 13 | "rating", 14 | "emotion", 15 | "star" 16 | ], 17 | "homepage": "https://rezaghz.com", 18 | "license": "MIT", 19 | "authors": [ 20 | { 21 | "name": "reza ghasemzadeh", 22 | "email": "rezaghasemzadeh1@gmail.com", 23 | "homepage": "https://rezaghz.com", 24 | "role": "Developer" 25 | } 26 | ], 27 | "require": { 28 | "php": ">=7.0", 29 | "illuminate/database": "^6.20.27|^7.0|^8.0|^9.0|^10.0", 30 | "illuminate/events": "^6.0|^7.0|^8.0|^9.0|^10.0", 31 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0" 32 | }, 33 | "require-dev": { 34 | "mockery/mockery": "^1.3.3", 35 | "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0", 36 | "phpunit/phpunit": "^7.5|^8.0|^9.0", 37 | "vimeo/psalm": "^4.0" 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "Rezaghz\\Laravel\\Reports\\": "src/" 42 | } 43 | }, 44 | "autoload-dev": { 45 | "psr-4": { 46 | "Rezaghz\\Tests\\Laravel\\Reports\\": "tests/" 47 | } 48 | }, 49 | "scripts": { 50 | "psalm": "vendor/bin/psalm", 51 | "test": "vendor/bin/phpunit --colors=always", 52 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage", 53 | "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" 54 | }, 55 | "config": { 56 | "sort-packages": true 57 | }, 58 | "extra": { 59 | "laravel": { 60 | "providers": [ 61 | "Rezaghz\\Laravel\\Reports\\ReportsServiceProvider" 62 | ] 63 | } 64 | }, 65 | "minimum-stability": "dev", 66 | "prefer-stable": true, 67 | "funding": [ 68 | { 69 | "type": "other", 70 | "url": "https://yekpay.me/en/rezaghz" 71 | } 72 | ] 73 | } 74 | -------------------------------------------------------------------------------- /migrations/2021_07_11_000000_create_reports_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 21 | $table->integer('user_id')->unsigned()->index(); 22 | $table->morphs('reportable'); 23 | $table->string('type')->nullable(); 24 | $table->timestamps(); 25 | $table->unique([ 26 | 'reportable_type', 27 | 'reportable_id', 28 | 'user_id', 29 | ], 'report_user_unique'); 30 | // $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 31 | }); 32 | } 33 | 34 | /** 35 | * Reverse the migrations. 36 | * 37 | * @return void 38 | */ 39 | public function down() 40 | { 41 | Schema::dropIfExists('reports'); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Contracts/ReportableInterface.php: -------------------------------------------------------------------------------- 1 | reportable = $reportable; 43 | $this->report = $report; 44 | $this->reportBy = $reportBy; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Events/OnReport.php: -------------------------------------------------------------------------------- 1 | reportable = $reportable; 43 | $this->report = $report; 44 | $this->reportBy = $reportBy; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidReportUser.php: -------------------------------------------------------------------------------- 1 | morphTo(); 37 | } 38 | 39 | /** 40 | * Get the user that reacted on Reportable model. 41 | * 42 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 43 | */ 44 | public function reportBy() 45 | { 46 | $userModel = config('auth.providers.users.model'); 47 | 48 | return $this->belongsTo($userModel, 'user_id'); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/ReportsServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadMigrations(); 17 | } 18 | 19 | /** 20 | * Register the service provider. 21 | * 22 | * @return void 23 | */ 24 | public function register() 25 | { 26 | } 27 | 28 | /** 29 | * Load migrations. 30 | * 31 | * @return void 32 | */ 33 | protected function loadMigrations() 34 | { 35 | if ($this->app->runningInConsole()) { 36 | $migrationsPath = __DIR__ . '/../migrations'; 37 | 38 | $this->publishes([ 39 | $migrationsPath => database_path('migrations'), 40 | ], 'migrations'); 41 | 42 | $this->loadMigrationsFrom($migrationsPath); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Traits/Reportable.php: -------------------------------------------------------------------------------- 1 | morphMany(Report::class, 'reportable'); 20 | } 21 | 22 | /** 23 | * Get collection of users who reported on reportable model. 24 | * 25 | * @return \Illuminate\Support\Collection 26 | */ 27 | public function reportsBy() 28 | { 29 | $userModel = $this->resolveUserModelObj(); 30 | 31 | $userIds = $this->reports->pluck('user_id'); 32 | 33 | return $userModel::whereKey($userIds)->get(); 34 | } 35 | 36 | /** 37 | * Attribute to get collection of users who reported on reportable model. 38 | * 39 | * @return \Illuminate\Support\Collection 40 | */ 41 | public function getReportsByAttribute() 42 | { 43 | return $this->reportsBy(); 44 | } 45 | 46 | /** 47 | * Report summary. 48 | * 49 | * @return \Illuminate\Database\Eloquent\Collection|static[] 50 | */ 51 | public function reportSummary() 52 | { 53 | return $this->reports->groupBy('type')->map(function ($val) { 54 | return $val->count(); 55 | }); 56 | } 57 | 58 | /** 59 | * Report summary attribute. 60 | * 61 | * @return \Illuminate\Database\Eloquent\Collection|static[] 62 | */ 63 | public function getReportSummaryAttribute() 64 | { 65 | return $this->reportSummary(); 66 | } 67 | 68 | /** 69 | * Add report. 70 | * 71 | * @param mixed $reportType 72 | * @param mixed $user 73 | * @return Reports|bool 74 | */ 75 | public function report($reportType, $user = null) 76 | { 77 | $user = $this->getUserObj($user); 78 | 79 | if ($user) { 80 | return $user->reportTo($this, $reportType); 81 | } 82 | 83 | return false; 84 | } 85 | 86 | /** 87 | * Remove report. 88 | * 89 | * @param mixed $user 90 | * @return bool 91 | */ 92 | public function removeReport($user = null) 93 | { 94 | $user = $this->getUserObj($user); 95 | 96 | if ($user) { 97 | return $user->removeReportFrom($this); 98 | } 99 | 100 | return false; 101 | } 102 | 103 | /** 104 | * Toggle Report. 105 | * 106 | * @param mixed $reportType 107 | * @param mixed $user 108 | * @return void|Reports 109 | */ 110 | public function toggleReport($reportType, $user = null) 111 | { 112 | $user = $this->getUserObj($user); 113 | 114 | if ($user) { 115 | return $user->toggleReportOn($this, $reportType); 116 | } 117 | } 118 | 119 | /** 120 | * Report on reportable model by user. 121 | * 122 | * @param mixed $user 123 | * @return Reports 124 | */ 125 | public function reported($user = null) 126 | { 127 | $user = $this->getUserObj($user); 128 | 129 | return $this->reports->where('user_id', $user->getKey())->first(); 130 | } 131 | 132 | /** 133 | * Report on reportable model by user. 134 | * 135 | * @return Reports 136 | */ 137 | public function getReportedAttribute() 138 | { 139 | return $this->reported(); 140 | } 141 | 142 | /** 143 | * Check is reported by user. 144 | * 145 | * @param mixed $user 146 | * @return bool 147 | */ 148 | public function isReportBy($user = null, $type = null) 149 | { 150 | $user = $this->getUserObj($user); 151 | 152 | if ($user) { 153 | return $user->isReportedOn($this, $type); 154 | } 155 | 156 | return false; 157 | } 158 | 159 | /** 160 | * Check is reported by user. 161 | * 162 | * @param mixed $user 163 | * @return bool 164 | */ 165 | public function getIsReportedAttribute() 166 | { 167 | return $this->isReportBy(); 168 | } 169 | 170 | /** 171 | * Fetch records that are reported by a given user. 172 | * 173 | * @param \Illuminate\Database\Eloquent\Builder $query 174 | * @param string $type 175 | * @param null|int|ReportsInterface $userId 176 | * @return \Illuminate\Database\Eloquent\Builder 177 | * 178 | * @throw InvalidReportUser 179 | * @todo think about method name 180 | * 181 | */ 182 | public function scopeWhereReportedBy(Builder $query, $userId = null, $type = null) 183 | { 184 | $user = null; 185 | 186 | try { 187 | $user = $this->getUserObj($userId); 188 | } catch (InvalidReportUser $e) { 189 | if (!$user && !$userId) { 190 | throw InvalidReportUser::notDefined(); 191 | } 192 | } 193 | 194 | $userId = ($user) ? $user->getKey() : $userId; 195 | 196 | return $query->whereHas('reports', function ($innerQuery) use ($userId, $type) { 197 | $innerQuery->where('user_id', $userId); 198 | 199 | if ($type) { 200 | $innerQuery->where('type', $type); 201 | } 202 | }); 203 | } 204 | 205 | /** 206 | * Get user model. 207 | * 208 | * @param mixed $user 209 | * @return ReportsInterface 210 | * 211 | * @throw \Qirolab\Laravel\Reports\Exceptions\InvalidReportUser 212 | */ 213 | private function getUserObj($user = null) 214 | { 215 | if (!$user && auth()->check()) { 216 | return auth()->user(); 217 | } 218 | 219 | if ($user instanceof ReportsInterface) { 220 | return $user; 221 | } 222 | 223 | if (!$user) { 224 | throw InvalidReportUser::notDefined(); 225 | } 226 | 227 | throw InvalidReportUser::invalidReportByUser(); 228 | } 229 | 230 | /** 231 | * Retrieve User's model class name. 232 | * 233 | * @return \Illuminate\Contracts\Auth\Authenticatable 234 | */ 235 | private function resolveUserModelObj() 236 | { 237 | return config('auth.providers.users.model'); 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /src/Traits/Reports.php: -------------------------------------------------------------------------------- 1 | reports()->where([ 22 | 'user_id' => $this->getKey(), 23 | ])->first(); 24 | 25 | if (!$report) { 26 | return $this->storeReport($reportable, $type); 27 | } 28 | 29 | if ($report->type == $type) { 30 | return $report; 31 | } 32 | 33 | $this->deleteReport($report, $reportable); 34 | 35 | return $this->storeReport($reportable, $type); 36 | } 37 | 38 | /** 39 | * Remove report from reportable model. 40 | * 41 | * @param ReportableInterface $reportable 42 | * @return void 43 | */ 44 | public function removeReportFrom(ReportableInterface $reportable) 45 | { 46 | $report = $reportable->reports()->where([ 47 | 'user_id' => $this->getKey(), 48 | ])->first(); 49 | 50 | if (!$report) { 51 | return; 52 | } 53 | 54 | $this->deleteReport($report, $reportable); 55 | } 56 | 57 | /** 58 | * Toggle report on reportable model. 59 | * 60 | * @param ReportableInterface $reportable 61 | * @param mixed $type 62 | * @return void 63 | */ 64 | public function toggleReportOn(ReportableInterface $reportable, $type) 65 | { 66 | $report = $reportable->reports()->where([ 67 | 'user_id' => $this->getKey(), 68 | ])->first(); 69 | 70 | if (!$report) { 71 | return $this->storeReport($reportable, $type); 72 | } 73 | 74 | $this->deleteReport($report, $reportable); 75 | 76 | if ($report->type == $type) { 77 | return; 78 | } 79 | 80 | return $this->storeReport($reportable, $type); 81 | } 82 | 83 | /** 84 | * Report on reportable model. 85 | * 86 | * @param ReportableInterface $reportable 87 | * @return Report 88 | */ 89 | public function ReportedOn(ReportableInterface $reportable) 90 | { 91 | return $reportable->reported($this); 92 | } 93 | 94 | /** 95 | * Check is reported on reportable model. 96 | * 97 | * @param ReportableInterface $reportable 98 | * @param mixed $type 99 | * @return bool 100 | */ 101 | public function isReportedOn(ReportableInterface $reportable, $type = null) 102 | { 103 | $isReported = $reportable->reports()->where([ 104 | 'user_id' => $this->getKey(), 105 | ]); 106 | 107 | if ($type) { 108 | $isReported->where([ 109 | 'type' => $type, 110 | ]); 111 | } 112 | 113 | return $isReported->exists(); 114 | } 115 | 116 | /** 117 | * Store report. 118 | * 119 | * @param ReportableInterface $reportable 120 | * @param mixed $type 121 | * @return Report 122 | */ 123 | protected function storeReport(ReportableInterface $reportable, $type) 124 | { 125 | $report = $reportable->reports()->create([ 126 | 'user_id' => $this->getKey(), 127 | 'type' => $type, 128 | ]); 129 | 130 | event(new OnReport($reportable, $report, $this)); 131 | 132 | return $report; 133 | } 134 | 135 | /** 136 | * Delete report. 137 | * 138 | * @param Report $report 139 | * @param ReportableInterface $reportable 140 | * @return void 141 | */ 142 | protected function deleteReport(Report $report, ReportableInterface $reportable) 143 | { 144 | $response = $report->delete(); 145 | 146 | event(new OnDeleteReport($reportable, $report, $this)); 147 | 148 | return $response; 149 | } 150 | } 151 | --------------------------------------------------------------------------------