Browse documentationOpen

byrcsc/laravel-comments · 1.x

Console commands.

comments:recount — the only command the package ships, its options, what a full sweep visits, and what it cannot find.

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's model events, so anything that goes around them — the query builder, an upsert, raw SQL, a restored database dump — leaves the column behind. This is the backstop for that, and the only thing in the package that reads a total rather than stepping one.

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.