Spam & blocklist filter
Configure banned words or regexes and decide how a match is handled:
// 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.
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:
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 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.