Preparing your models
Add the GivesReports trait to models that file reports, and HasReports to models that can be reported:
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Reports\Contracts\Reportable;
use RoundlyConsulting\Reports\Contracts\Reporter;
use RoundlyConsulting\Reports\Traits\GivesReports;
use RoundlyConsulting\Reports\Traits\HasReports;
class User extends Model implements Reporter
{
use GivesReports;
}
class Post extends Model implements Reportable
{
use HasReports;
}Reporter and Reportable are empty marker interfaces — implement them for type-hinting if you like; the traits do the work.
What GivesReports adds
$user->givenReports; // every report the user has filed
$user->givenReports()->pending()->count(); // relation + Report scopes
$user->giveReportTo($post); // file a report — returns the Report
$user->report($post); // a PendingReport with the reporter pre-set
// (both go through the manager, so Reports::fake() records them)- givenReports() — a MorphMany of every report the model has filed.
- giveReportTo($model, ?$description, ?$reason) — file a report in one call and get the Report back.
- report($model) — start a PendingReport with this model as the reporter.
What HasReports adds
$post->reports; // every report against the post
$post->pendingReports; // open reports only — Pending and InReview
$post->reportsCount(); // int
$post->hasBeenReported(); // bool
$post->isReportedBy($user); // bool- reports() and pendingReports() — MorphMany relations; pendingReports covers both open statuses.
- reportsCount(), hasBeenReported(), isReportedBy() — counting and lookup helpers.
- withReportCounts(), mostReported(), reportedMoreThan() — query scopes (see Aggregation & querying).
A model that files and receives reports
Users often report other users. Both traits define the same private reportModel() helper, so PHP refuses to compose them on one class until you resolve the collision. The two implementations are identical, so either can win:
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Reports\Contracts\Reportable;
use RoundlyConsulting\Reports\Contracts\Reporter;
use RoundlyConsulting\Reports\Traits\GivesReports;
use RoundlyConsulting\Reports\Traits\HasReports;
class User extends Model implements Reporter, Reportable
{
use GivesReports, HasReports {
HasReports::reportModel insteadof GivesReports;
}
}Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.