---
title: "Aggregation & querying — Reports for Laravel | Roundly"
description: "Count reports per subject, check who reported what, and sort or filter subjects by report count with ready-made scopes."
url: https://roundly-consulting.com/open-source/docs/reports-for-laravel/aggregation
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Reports for Laravel](https://roundly-consulting.com/open-source/docs/reports-for-laravel.md)

# Aggregation & querying

HasReports adds counting and lookup helpers to every reportable model:

```php
use RoundlyConsulting\Reports\Enums\Status;

$post->reportsCount();                 // total
$post->reportsCount(Status::Pending);  // a specific status
$post->hasBeenReported();
$post->isReportedBy($user);
$post->pendingReports;                 // open reports relation
```

## Query scopes

```php
Post::query()->withReportCounts()->get();                    // eager reports_count
Post::query()->mostReported()->get();                        // by total report count
Post::query()->mostReported(Status::Pending)->get();         // count only pending
Post::query()->reportedMoreThan(5)->get();                   // more than 5 reports
Post::query()->reportedMoreThan(5, Status::Pending)->get();  // more than 5 pending
```

- withReportCounts() — eager-loads reports\_count.
- mostReported(?Status) — adds reports\_count and orders by it, highest first; pass a status to count only that status.
- reportedMoreThan(int, ?Status) — subjects with strictly more reports than the number given, optionally within one status; reports\_count is included.

reportedMoreThan() filters through whereHas rather than HAVING on the count alias, so it runs on PostgreSQL and MySQL as well as SQLite.

## Querying reports directly

The Report model ships its own scopes — withStatus, withReason, pending, resolved, rejected and open — for moderation queues and dashboards:

```php
use RoundlyConsulting\Reports\Enums\Status;
use RoundlyConsulting\Reports\Models\Report;

// A moderation queue: every open report, newest first.
Report::query()->open()->with(['reporter', 'reported'])->latest()->paginate(25);

Report::query()->withStatus(Status::InReview)->get();
Report::query()->withReason('harassment')->get();
Report::query()->pending()->count();
Report::query()->resolved()->get();
Report::query()->rejected()->get();
```

## 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 crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## 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.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/reports-for-laravel.md)
