Browse documentationOpen

byrcsc/laravel-comments · 1.x

Installation and setup.

Install Laravel Comments, choose actor key types before migrating, publish the schema, and make a model commentable.

Laravel Comments requires PHP 8.3 or 8.4 and Laravel 12 or 13.

Install in this order — configuration first, migration last:

composer require byrcsc/laravel-comments
php artisan vendor:publish --tag="comments-config"
php artisan vendor:publish --tag="comments-migrations"
php artisan migrate

Laravel discovers the CommentsServiceProvider automatically. There is no facade and no alias to register.

Publish the configuration before you migrate. Table names and actor key types are read by the migration when it runs. Migrating on the defaults and changing config/comments.php afterwards leaves a schema that does not match the configuration, and that is your own migration to write.

Four migrations publish, one per table: comments, comment_reactions, comment_revisions, and comment_attachments. They reference each other through foreign keys, so a partial install is not a useful state to be in.

Make a model commentable

Add the trait. That is the whole integration:

use ByRcsc\LaravelComments\Concerns\HasComments;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasComments;
}

The model gains a comments() relation and two write methods. Nothing else needs wiring. See commentable models.

Actor key types

The polymorphic identity columns — the commentator on a comment, the reactor on a reaction, the editor on a revision — are written with the key type named by comments.actor_key_type. Set it before migrating:

COMMENTS_ACTOR_KEY_TYPE=uuid
ValueColumn type
intunsignedBigInteger
uuiduuid
ulidulid
stringstring

The commentable model's key is independent of this setting. The comments table writes commentable_id through Laravel's morphs() helper, which is an integer column; change it in the published migration when the models being commented on do not use integer keys.

Changing the key type after data exists requires your own migration.

Environment reference

VariableDefaultPurpose
COMMENTS_ACTOR_KEY_TYPEintKey type of every polymorphic identity column
COMMENTS_ATTACHMENTS_DISKunsetDefault disk for attachImage(), or the app default
COMMENTS_NOTIFY_REPLIESfalseWhether the shipped reply notification delivers

Everything else lives directly in config/comments.php. See configuration for the full key reference.

Rename tables

Set the names in configuration rather than in the migration; the models read the same values:

'table_names' => [
    'comments' => 'discussion_comments',
    'comment_reactions' => 'discussion_comment_reactions',
    'comment_revisions' => 'discussion_comment_revisions',
    'comment_attachments' => 'discussion_comment_attachments',
],

Every key must still be present. A missing or blank name throws an InvalidConfigurationException when the package boots, not on the first comment. Renaming a table that already holds comments requires your own migration.

Optional: notification wording and views

Neither is needed to install the package, and both are worth leaving alone until you actually want to change something:

php artisan vendor:publish --tag="comments-translations"
php artisan vendor:publish --tag="comments-views"

The first writes lang/vendor/comments/en/comments.php for rewording and translating; the second writes resources/views/vendor/comments/mail/ for restructuring the reply email. See reply notifications.

Optional: image attachments

attachImage() needs the framework's Image facade, which arrived in Laravel 13, and intervention/image, which the package suggests rather than requires:

composer require intervention/image

Without it, attachImage() throws an ImageSupportMissingException naming the missing dependency. Nothing else in the package touches it — attach() records metadata about a file you already stored and needs no image library at all. See attachments.

Verify the installation

Write a comment in php artisan tinker, or run the repair command, which resolves the schema and reports without changing anything:

php artisan comments:recount --dry-run

On a fresh install with no counted models it prints Every count was already correct. and exits successfully.