›
›
›
  1. docs
  2. ›
  3. byrcsc/laravel-comments
1.x
Browse documentationOpenClose

Getting started

  • Introduction
  • Installation and setup
  • Quick start

Core concepts

  • Commentable models
  • Threads and replies
  • Moderation
  • Initial status
  • Pinning
  • Reactions
  • Edits and revisions
  • Attachments
  • Deleting comments

Operations

  • Comment counts
  • Events and listeners
  • Reply notifications
  • Authorization
  • Rendering and safety

Reference

  • Configuration
  • Console commands
  • Testing
  • Troubleshooting

Getting started

  • Introduction
  • Installation and setup
  • Quick start

Core concepts

  • Commentable models
  • Threads and replies
  • Moderation
  • Initial status
  • Pinning
  • Reactions
  • Edits and revisions
  • Attachments
  • Deleting comments

Operations

  • Comment counts
  • Events and listeners
  • Reply notifications
  • Authorization
  • Rendering and safety

Reference

  • Configuration
  • Console commands
  • Testing
  • Troubleshooting

byrcsc/laravel-comments · 1.x

Console commands.

Rebuild stored comment counts through Artisan.

The package ships one command.

comments:recount

Recomputes denormalized comment counts on commentable models that keep one.

php artisan comments:recount
OptionPurpose
--model=Only this commentable, by class name or morph alias
--id=Only this record. Needs --model too
--dry-runReport what would change without writing anything

Counts are maintained through Eloquent model events. The query builder, an upsert, raw SQL, or a restored database dump can bypass those events and leave the column behind. This command recalculates the total from the database.

Output

It recomputes from one grouped query per model type and writes only the rows that disagree, listing each one:

  App\Models\Post #12 ....................................... 3 → 5
  App\Models\Post #41 .................................... null → 0

  INFO  Repaired 2 count(s).

Against a correct table it says so and writes nothing:

  INFO  Every count was already correct.

Exit status is 0 on success, 1 on a usage error.

Repair one model or one record

php artisan comments:recount --model="App\Models\Post"
php artisan comments:recount --model=post
php artisan comments:recount --model="App\Models\Post" --id=42

--model takes a class name or a morph alias your application has registered. --id on its own is refused: a key does not say which table to look in.

The command fails, rather than passing quietly, when the named model cannot be resolved or keeps no count. That is the difference between naming a model and sweeping everything, an explicit --model is worth an error.

Check before writing

php artisan comments:recount --dry-run

Prints the same per-row detail and reports n count(s) would change. without writing.

What a full sweep visits

Every commentable type the comments table mentions, tombstones included, plus every alias in your application's morph map.

Tombstones matter because a type whose comments are all soft deleted still owes its records a zero, and the ordinary scope would hide it. The morph map covers the case the table cannot describe at all, every comment hard deleted, no row left to name the type.

Types that keep no count, and types that resolve to nothing, are skipped quietly.

A sweep cannot find a type it has never been shown. If every comment on a model has been hard deleted and your application registers no morph map, no row and no alias names that class, run --model for it explicitly.

Schedule it

Reasonable insurance for an application that also writes comments outside Eloquent:

use Illuminate\Support\Facades\Schedule;

Schedule::command('comments:recount')->dailyAt('03:00');

The command is idempotent, so the interval is a floor on how long drift can survive rather than a correctness knob.

Repair one record in code

$post->recountComments();  // returns the value now stored

Same computation, one record, no console. Throws a CommentsCountNotEnabledException on a model that keeps no count. See comment counts.

What to read next

  • Comment counts to enable and maintain stored totals.
  • Events and listeners to understand which writes normally update the count.
  • Troubleshooting when a recount cannot discover a model.
PreviousConfigurationNextTesting
View source

On this page

  1. comments:recount
  2. Output
  3. Repair one model or one record
  4. Check before writing
  5. What a full sweep visits
  6. Schedule it
  7. Repair one record in code
  8. What to read next