ghanem / reportable
为 Laravel 6 & 7 设计的可报告多态 Eloquent 模型
v1.2
2021-01-24 02:20 UTC
Requires
- php: >=7.2.0
- illuminate/support: ^6.0 | ^7.0 | ^8.0 | ^9.0
This package is auto-updated.
Last update: 2024-09-24 10:26:17 UTC
README
Laravel Reportable
此包允许您将完整的报告系统添加到您的 Laravel 应用程序中。
安装
首先,通过 Composer 拉取包。
composer require ghanem/reportable
然后,在 app/config/app.php
中包含服务提供者。
'providers' => [ Ghanem\Reportable\ReportableServiceProvider::class ];
最后,您需要发布并运行迁移。
php artisan vendor:publish --provider="Ghanem\Reportable\ReportableServiceProvider"
并且
php artisan migrate
设置模型
<?php namespace App; use Ghanem\Reportable\Contracts\Reportable; use Ghanem\Reportable\Traits\Reportable as ReportableTrait; use Illuminate\Database\Eloquent\Model; class Post extends Model implements Reportable { use ReportableTrait; }
示例
用户模型报告帖子模型
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\Post; use Auth; class PostController extends Controller { public function makeReport() { $post = Post::find(1); $user = Auth::user(); $post->report([ 'reason' => str_random(10), 'meta' => ['some more optional data, can be notes or something'], ], $user); }
为报告创建结论并添加用户模型作为“裁判”(以后查看谁或什么得出这个结论很有用)
$report->conclude([ 'conclusion' => 'Your report was valid. Thanks! We\'ve taken action and removed the entry.', 'action_taken' => 'Record has been deleted.' // This is optional but can be useful to see what happend to the record 'meta' => ['some more optional data, can be notes or something'], ], $user);
获取报告模型的结论
$report->conclusion;
获取报告模型的裁判(只有当有结论时才可用)
$report->judge(); // Just a shortcut for $report->conclusion->judge
获取一个包含所有曾经“裁判”过某事的裁判的数组
Report::allJudges();