rezaghz / laravel-reports
Laravel 报告包,用于在 Eloquent 模型上实现报告(例如:垃圾邮件、暴力、儿童虐待、非法毒品等)。
Requires
- php: >=7.0
- illuminate/database: ^6.20.27|^7.0|^8.0|^9.0
- illuminate/events: ^6.0|^7.0|^8.0|^9.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^4.0|^5.0|^6.0|^7.0
- phpunit/phpunit: ^7.5|^8.0|^9.0
- vimeo/psalm: ^4.0
This package is auto-updated.
Last update: 2024-09-28 15:39:27 UTC
README
Laravel 报告包,用于在 Eloquent 模型上实现报告(例如:垃圾邮件、暴力、儿童虐待、非法毒品等)。
安装
使用 Composer 将包下载到项目中。
$ composer require rezaghz/laravel-reports
注册包
Laravel 5.5(或更高版本)使用包自动发现,因此不需要您手动添加 ServiceProvider。
对于 Laravel 5.4 或更早版本,请在 app/config/app.php
中包含 service provider。
'providers' => [ Rezaghz\Laravel\Reports\ReportsServiceProvider::class, ],
数据库迁移
如果您要更改迁移,请先将其发布到您的应用程序。
$ php artisan vendor:publish --provider="Rezaghz\Laravel\Reports\ReportsServiceProvider" --tag=migrations
运行数据库迁移。
$ php artisan migrate
用法
准备报告(用户)模型
在将执行报告行为的报告模型中,使用 Rezaghz\Laravel\Reports\Contracts\ReportsInterface
接口,并实现它,然后使用 Rezaghz\Laravel\Reports\Traits\Reports
特性。
use Rezaghz\Laravel\Reports\Traits\Reports; use Rezaghz\Laravel\Reports\Contracts\ReportsInterface; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements ReportsInterface { use Reports; }
准备报告模型
在将获取报告行为的模型中使用 Rezaghz\Laravel\Reports\Contracts\ReportableInterface
接口,并实现它,然后使用 Rezaghz\Laravel\Reports\Traits\Reportable
特性。
use Illuminate\Database\Eloquent\Model; use Rezaghz\Laravel\Reports\Traits\Reportable; use Rezaghz\Laravel\Reports\Contracts\ReportableInterface; class Article extends Model implements ReportableInterface { use Reportable; }
可用方法
报告
$user->reportTo($article, 'spam'); $article->report('spam'); // current login user $article->report('spam', $user);
移除报告
从报告模型中移除用户的报告。
$user->removeReportFrom($article); $article->removeReport(); // current login user $article->removeReport($user);
切换报告
切换报告方法会在用户未报告时向模型添加报告。如果用户已报告,则将替换之前的报告为新的报告。例如,如果用户已报告模型上的 '垃圾邮件'。现在切换报告为 '暴力',则将移除 '垃圾邮件' 并存储 '暴力' 报告。
如果用户已报告 垃圾邮件
,则在切换报告为 垃圾邮件
时,将移除报告。
$user->toggleReportOn($article, 'spam'); $article->toggleReport('spam'); // current login user $article->toggleReport('spam', $user);
布尔值检查用户是否已对模型进行报告
$user->isReportedOn($article)); $article->is_reported; // current login user $article->isReportBy(); // current login user $article->isReportBy($user);
模型上的报告摘要
$article->reportSummary(); $article->report_summary; // example $article->report_summary->toArray(); // output /* [ "spam" => 5, "violence" => 2, "illegal_drugs" => 4, "child_abuse" => 1 ] */
获取对模型进行报告的用户集合
$article->reportsBy();
作用域
查找用户报告的所有文章。
Article::whereReportedBy()->get(); // current login user Article::whereReportedBy($user)->get(); Article::whereReportedBy($user->id)->get();
模型上的报告
// It will return the Report object that is reported by given user. $article->reported($user); $article->reported(); // current login user $article->reported; // current login user $user->reportedOn($article);
事件
每次添加报告时,都会触发 \Rezaghz\Laravel\Reports\Events\OnReport
事件。
每次删除报告时,都会触发 \Rezaghz\Laravel\Reports\Events\OnDeleteReport
事件。
测试
运行测试
$ vendor/bin/phpunit