---
title: "Spam & blocklist filter — Comments for Laravel | Roundly"
description: "Reject, hold or hide comments that match banned words or regular expressions, or check a body yourself — no external service needed."
url: https://roundly-consulting.com/open-source/docs/comments-for-laravel/blocklist
language: en
---

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

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

# Spam & blocklist filter

Configure banned words or regexes and decide how a match is handled:

```php
// config/comments.php
'blocklist' => ['spam', '/casino|viagra/i'],
'blocklist_action' => 'reject', // 'reject' | 'pending' | 'hidden'
```

- Plain strings match case-insensitively as whole words — spam matches “Spam!” but not “spammer”.
- Delimited entries such as /casino|viagra/i are used as regular expressions, as written.
- A blank or non-string entry throws an InvalidConfigurationException naming comments.blocklist.

## Actions

| Action | A matching new comment is… | A matching edit… |
| --- | --- | --- |
| `reject` | Not stored — CommentRejectedException is thrown. | Is refused — CommentRejectedException is thrown. |
| `pending` | Stored with the pending status, awaiting review. | Moves an approved comment to pending; a hidden one stays hidden. |
| `hidden` | Stored with the hidden status. | Moves the comment to hidden. |

Edits run the same filter, so a comment posted clean cannot be edited into spam and stay public. An edit only ever tightens the status — it never lifts a hidden comment to pending, and a clean edit leaves a held comment for a moderator to approve.

```php
use RoundlyConsulting\Comments\Exceptions\CommentRejectedException;

try {
    Comments::on($post)->as($user)->body($body)->post();
} catch (CommentRejectedException $e) {
    // $e->getMessage(): "This comment contains content that is not allowed."
}
```

## Checking a body yourself

Comments::blocklisted($body) asks the same filter directly: whether the body matches a configured word or pattern, checked exactly as a write checks it (trimmed). It ignores blocklist\_action and never rejects, so it answers under reject too — only a malformed blocklist setting still throws InvalidConfigurationException. Use it to tell a comment the blocklist held as pending apart from one held by require\_approval — before auto-approving it, for example — or to warn a user before they post. It is a read, so Comments::fake() records nothing for it:

```php
use RoundlyConsulting\Comments\Enums\CommentStatus;
use RoundlyConsulting\Comments\Facades\Comments;

$comment = Comments::on($ticket)->as($user)->body($text)->post();

if ($comment->status === CommentStatus::Pending && ! Comments::blocklisted($comment->comment)) {
    Comments::approve($comment);   // held only by require_approval, not by the blocklist
}
```

## 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/comments-for-laravel.md)
