laravel-comments is a free, open source collaboration & feedback project written in PHP and released under MIT. It has 602 GitHub stars, 98 forks and 4 open issues, and was last pushed 22 hours ago. On this registry it ranks #16 of 21 tracked projects in Collaboration & Feedback, with 5 head-to-head comparisons available.

What is laravel-comments?

laravel-comments is a PHP package for Laravel developers who want to attach an approval-aware, nestable commenting system to their Eloquent models without building one from scratch.

What it is

laravel-comments is an MIT-licensed Composer package, published as beyondcode/laravel-comments on Packagist and maintained by the team behind beyondco.de, that lets any Laravel Eloquent model accept comments. You opt a model in by adding the BeyondCode\Comments\Traits\HasComments trait, after which the model gains a comments relationship and comment / commentAsUser methods for creating comments directly from application code. Comments are stored through a migration the package ships and are configured through a publishable config/comments.php file.

The concrete problem it solves is the recurring need to attach user discussion to domain objects — posts, orders, tickets, anything you model — without hand-rolling a comment table, an approval workflow, and reply threading for every project. It replaces that bespoke build with a single trait, an is_approved boolean flag for moderation, and a Comment model that itself implements HasComments, so replies nest for free rather than requiring a self-referential relationship you write yourself.

Key capabilities

  • Makes any Eloquent model commentable by adding the HasComments trait, exposing a comments relation on that model.
  • Creates comments with $post->comment('This is a comment'), returning the newly created comment instance.
  • Attributes comments to an explicit author via commentAsUser($user, ...), so you can comment on behalf of another user.
  • Moderates comments through the is_approved flag, with a dedicated approve() method on the Comment model and an approved() query scope for filtering, e.g. $post->comments()->approved()->get().
  • Auto-approves per user by implementing the BeyondCode\Comments\Contracts\Commentator interface; needsCommentApproval($model) returns a boolean deciding whether a given comment starts approved or unapproved.
  • Nests replies because BeyondCode\Comments\Comment uses the HasComments trait itself, allowing comments on comments.
  • Optionally cascades reply deletion via the delete_replies_along_comments setting in config/comments.php.
  • Registers and configures itself through its CommentsServiceProvider, with migrations and config published through php artisan vendor:publish.

Who uses it and how

  • Laravel application developers adding a discussion layer to content models such as blog posts, using $post->comment(...) directly in controllers or service classes.
  • Teams that need comment moderation, keeping new comments hidden until $comment->approve() is called or until an unapproved flag is filtered out in views and controllers.
  • Projects where comments must be attributed to an existing user record rather than the request context, handled with commentAsUser.
  • Applications with varying trust levels, where a User model implements Commentator to skip approval for staff or trusted accounts while ordinary users' comments are held for review.
  • Builds that require threaded discussion, using replies-to-comments with optional cascading deletion of those replies.

Getting started

Install it with composer require beyondcode/laravel-comments, then publish the migration and config with php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="migrations" and --tag="config", and run php artisan migrate.

How it compares

Among the tools named in these facts — Laravel itself and the comments topic space — the package occupies a narrow, specific slot: it supplies commenting as a Composer dependency inside an existing Laravel application rather than as a standalone service or platform. Its identity is defined by what it integrates with (Eloquent models, Artisan, Packagist) rather than by any competitor named alongside it, so it stands alone in this registry.

When to use it — and when not

You should pick it when you already run a Laravel application and are content to operate a standard relational database for comment storage, publishing and running the package's migrations yourself; there is no hosted option, no bundled frontend, and no email delivery — views, controllers, and moderation UI remain yours to build. Skip it if you are not on Laravel, or if you need moderation tooling, spam filtering, notifications, or a ready-made UI out of the box, none of which the README describes; note also that the README excerpt ends mid-sentence, so the full feature set is not entirely documented here.

project readme (upstream, from github) — read inline

Add comments to your Laravel application

Latest Version on Packagist Total Downloads

Add the ability to associate comments to your Laravel Eloquent models. The comments can be approved and nested.

$post = Post::find(1);

$post->comment('This is a comment');

$post->commentAsUser($user, 'This is a comment from someone else');

Installation

You can install the package via composer:

composer require beyondcode/laravel-comments

The package will automatically register itself.

You can publish the migration with:

php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="migrations"

After the migration has been published you can create the media-table by running the migrations:

php artisan migrate

You can publish the config-file with:

php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="config"

Usage

Registering Models

To let your models be able to receive comments, add the HasComments trait to the model classes.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use BeyondCode\Comments\Traits\HasComments;

class Post extends Model
{
    use HasComments;
    ...
}

Creating Comments

To create a comment on your commentable models, you can use the comment method. It receives the string of the comment that you want to store.

$post = Post::find(1);

$comment = $post->comment('This is a comment from a user.');

The comment method returns the newly created comment class.

Sometimes you also might want to create comments on behalf of other users. You can do this using the commentAsUser method and pass in your user model that should get associated with this comment:

$post = Post::find(1);

$comment = $post->commentAsUser($yourUser, 'This is a comment from someone else.');

Approving Comments

By default, all comments that you create are not approved - this is just a boolean flag called is_approved that you can use in your views/controllers to filter out comments that you might not yet want to display.

To approve a single comment, you may use the approve method on the Comment model like this:

$post = Post::find(1);
$comment = $post->comments->first();

$comment->approve();

Auto Approve Comments

If you want to automatically approve a comment for a specific user (and optionally model) you can let your User model implement the following interface and method:

namespace App\Models;

use BeyondCode\Comments\Contracts\Commentator;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements Commentator
{
    /**
     * Check if a comment for a specific model needs to be approved.
     * @param mixed $model
     * @return bool
     */
    public function needsCommentApproval($model): bool
    {
        return false;
    }

}

The needsCommentApproval method received the model instance that you want to add a comment to and you can either return true to mark the comment as not approved, or return false to mark the comment as approved.

Retrieving Comments

The models that use the HasComments trait have access to it's comments using the comments relation:


$post = Post::find(1);

// Retrieve all comments
$comments = $post->comments;

// Retrieve only approved comments
$approved = $post->comments()->approved()->get();

Nesting Comments

BeyondCode\Comments\Comment itself implements the HasComments trait, so you can comment on a comment and therefore nest them:

$comment = BeyondCode\Comments\Comment::first();
$comment->commentAsUser($user, "Hey there!");
Deleting Replies

When you delete a comment, you may optionally want to delete all its nested comments (replies). To optionally enable this feature, set the delete_replies_along_comments config property in the config/comments.php file to true.

Events

When a new comment is added the BeyondCode\Comments\Events\CommentAdded event will be dispatched. When a comment is deleted the BeyondCode\Comments\Events\CommentDeleted event will be dispatched.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Frequently asked questions

Is laravel-comments free to use?

laravel-comments is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does laravel-comments do?

Add comments to your Laravel application

What is laravel-comments written in?

laravel-comments is primarily written in PHP. Its source is publicly available at https://github.com/beyondcode/laravel-comments, and it has 602 GitHub stars.