PHPackages                             rubik-llc/laravel-comments - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rubik-llc/laravel-comments

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rubik-llc/laravel-comments
==========================

Attach comments to Eloquent Models

v0.1.0(4y ago)4132[3 PRs](https://github.com/rubik-llc/laravel-comments/pulls)MITPHPPHP ^8.0

Since Mar 17Pushed 2y ago2 watchersCompare

[ Source](https://github.com/rubik-llc/laravel-comments)[ Packagist](https://packagist.org/packages/rubik-llc/laravel-comments)[ Docs](https://github.com/rubik-llc/laravel-comments)[ GitHub Sponsors](https://github.com/rubik-llc)[ RSS](/packages/rubik-llc-laravel-comments/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (13)Versions (5)Used By (0)

Laravel comments
================

[](#laravel-comments)

[![Platform](https://camo.githubusercontent.com/69c09f29de04e9813d7b160a87878f515b9ba71c4e2d47212f8eb91e59c65604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6c61726176656c2d726564)](https://camo.githubusercontent.com/69c09f29de04e9813d7b160a87878f515b9ba71c4e2d47212f8eb91e59c65604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6c61726176656c2d726564)[![Latest Version on Packagist](https://camo.githubusercontent.com/10201e86e205f27aa9c354bc0bd8a1d27129bb4a4234badcaedd03a385e2374b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f727562696b2d6c6c632f6c61726176656c2d636f6d6d656e74732e737667)](https://packagist.org/packages/rubik-llc/laravel-comments)[![GitHub Workflow Status](https://camo.githubusercontent.com/4026ea06532c6bf140b0ca112b3bb76d1cd23106c1ac815c9f686a250e7ccb4f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f727562696b2d6c6c632f6c61726176656c2d636f6d6d656e74732f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/rubik-llc/laravel-comments/actions/workflows/run-tests.yml)[![Check & fix styling](https://camo.githubusercontent.com/6808229452d33dc5921a9b68b595d8e69dc2a58e5f4ed0bf53806b3a81654144/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f727562696b2d6c6c632f6c61726176656c2d636f6d6d656e74732f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636865636b253230616e642532306669782532307374796c696e67)](https://github.com/rubik-llc/laravel-comments/actions/workflows/php-cs-fixer.yml)[![GitHub](https://camo.githubusercontent.com/c07b524f95ed83bfb85425116f3bbaefb09f46651aa50d2b9fb434aca961c2aa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f727562696b2d6c6c632f6c61726176656c2d636f6d6d656e7473)](LICENSE.md)

This package enables to easily associate comments to any Eloquent model in your Laravel application.

```
//Associate a comment to a model as a logged in user
$post->comment('My comment!');

//Associate a comment to a model as a specific user
$post->commentAs($user, "Another user's comment!");
```

```
//Associate a comment to a model directly from the user
$user->commentTo($post, 'Comment from user!');
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require rubik-llc/laravel-comments
```

Publish and run the migrations with:

```
php artisan vendor:publish --tag="comments-migrations"
php artisan migrate
```

Alternatively, you can publish the config file with:

```
php artisan vendor:publish --tag="comments-config"
```

This is the contents of the published config file:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Comment class
    |--------------------------------------------------------------------------
    |
    | The comment class that should be used to store and retrieve the comments.
    | If you specify a different model class, make sure that model extends the default
    | Comment model that is shipped with this package.
    |
    */

    'comment_model' => \Rubik\LaravelComments\Models\Comment::class,

    /*
    |--------------------------------------------------------------------------
    | Needs approval
    |--------------------------------------------------------------------------
    |
    | By default, when creating comments they don't need approval (unless specified otherwise).
    | You can change the default value here.
    |
    */

    'needs_approval' => false,

    /*
    |--------------------------------------------------------------------------
    | Cascade on delete
    |--------------------------------------------------------------------------
    |
    | When this option is enabled, all related comments will be deleted when
    | the commentable class is deleted.
    |
    | If you want to overwrite this config for a specific class, you need to add
    | "$cascadeCommentsOnDelete" property to the commentable class.
    |
    | E.g:
    |
    |   class Commentable extends Model
    |   {
    |       use HasComments;
    |
    |       public static bool $cascadeCommentsOnDelete = false;
    |       ...
    |
    */

    'cascade_on_delete' => true,

    /*
    |--------------------------------------------------------------------------
    | Commenter name attribute
    |--------------------------------------------------------------------------
    |
    | The default attribute that returns the name of the commenter. Every class
    | that uses the "CanComment" trait will have the "commenter_name" attribute
    | appended, which will return the value of the attribute specified here.
    |
    | If you want to overwrite this config for a specific class, you need to add
    | "$nameAttribute" property to the commenter class.
    |
    | E.g:
    |
    |   class Commenter extends Model
    |   {
    |       use CanComment;
    |
    |       public string $nameAttribute = 'username';
    |       ...
    |
    */

    'commenter_name_attribute' => 'name',

    /*
    |--------------------------------------------------------------------------
    | Silence name attribute exception
    |--------------------------------------------------------------------------
    |
    | By default, if the class that uses the "CanComment" trait doesn't have the
    | name attribute specified in the "commenter_name_attribute" option or in
    | the "$nameAttribute" property, an exception will be thrown. If you enable
    | this option, no exception will be thrown and the "commenter_name" attribute
    | will return "null" if it can't find the specified name attribute.
    |
    */

    'silence_name_attribute_exception' => false,

];
```

Usage
-----

[](#usage)

### Registering the Commentable Model

[](#registering-the-commentable-model)

In order to let your models have comments associated to them, simply add the `HasComments` trait to the class of that model.

```
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Rubik\LaravelComments\Traits\HasComments;

class Post extends Model
{
    use HasComments;

    ...
}
```

In addition to the configuration, you can specify whether the comments associated to a commentable class should be deleted when the commentable model is deleted by adding the `$cascadeCommentsOnDelete` property to the class.

```
class Post extends Model
{
    use HasComments;

    public static bool $cascadeCommentsOnDelete = true;

    ...
}
```

### Registering the Commenter Model

[](#registering-the-commenter-model)

In order to let a model be able to attach comments, add the `CanComment` trait to the class of that model.

```
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Rubik\LaravelComments\Traits\CanComment;

class User extends Model
{
    use CanComment;

    ...
}
```

The `CanComment` trait appends a `commenter_name` attribute which returns the value of the attribute specified in the config file. In addition to that you can specify the name attribute, the value of which the `commenter_name` will have by adding the `$nameAttribute` property in the commenter class.

```
class User extends Model
{
    use CanComment;

    public string $nameAttribute = 'username';

    ...
}
```

In this case `$user->commenter_name` will return the same value as `$user->username`.

### Creating comments

[](#creating-comments)

1. To create a comment for the currently logged in user you can use the following syntax.

```
$post->comment('First comment!');
```

Additionally, you can specify whether a comment needs to be approved by adding a bool value as a second parameter.

```
$post->comment('Comment with default configuration!'); // this comment's approval is required based on configuration

$post->comment('Comment with approval!', true); // this comment needs to be approved

$post->comment('Comment with no approval!', false); // this comment doesn't need to be approved
```

2. You can create comments as other users.

```
$user = User::find(1);

$post->commentAs($user, 'First comment!');

$post->commentAs($user, 'Second comment!', true); // this comment needs to be approved
```

3. Eventually, comments can bre created directly from the user model.

```
$post = Post::find(1);

$user->commentTo($post, 'First comment!');

$user->commentTo($post, 'Second comment!', true); // this comment needs to be approved
```

### Retrieving comments

[](#retrieving-comments)

1. Retrieving comments from the commentable model.

```
$post = Post::find(1);

// Retrieve all comments
$post->comments;

// Retrieve only approved comments and those that don't need approval
$post->approvedComments;
```

2. Retrieving comments from the commenter model.

```
$user = User::find(1);

// Retrieve all comments
$user->comments;

// Retrieve only approved comments and those that don't need approval
$user->approvedComments;
```

### Retrieving the commentable

[](#retrieving-the-commentable)

```
$comment = Comment::find(1);

// Retrieve the commentable model instance
$comment->commentable;
```

### Retrieving the commenter

[](#retrieving-the-commenter)

```
$comment = Comment::find(1);

// Retrieve the commenter model instance
$comment->commenter;
```

### Checking if comments are approved

[](#checking-if-comments-are-approved)

To quickly check if a comment is approved use the `is_approved` attribute, it will return true if a comment is approved or doesn't need approval, otherwise it will return false.

```
$comment->is_approved // true/false
```

### Approving comments

[](#approving-comments)

```
$comment = Comment::find(1);

$comment->approve();

$comment->approved_at // will return the date when the comment was approved
```

The `approve()` method accepts a string or a `Carbon` instance as a parameter to specify the `approved_at` date.

```
$comment->approve('2022-01-01');

$comment->approved_at // will return '2022-01-01'
```

```
$comment->approve(Carbon::parse('2022-02-02'));

$comment->approved_at // will return '2022-02-02'
```

By default, if you approve an already approved comment the `approved_at` value won't change.

```
$comment->approved_at // '2020-01-18'

$comment->approve('2022-01-01');

$comment->approved_at // will return '2020-01-18'
```

You can overwrite the `approved_at` by adding a boolean value as a second parameter.

```
$comment->approved_at // '2020-01-18'

$comment->approve('2022-01-01', true);

$comment->approved_at // will return '2022-01-01'
```

### Disapproving comments

[](#disapproving-comments)

```
$comment = Comment::find(1);

$comment->approved_at // '2022-01-01'

$comment->dissapprove();

$comment->approved_at // will return null
```

### Replying to comments

[](#replying-to-comments)

Since the `Comment` class uses the `HasComments` trait, it is possible to attach comments to other comments.

```
$comment = Comment::find(1);

$comment->comment('This is a reply for the first comment!')

$comment->comments // will return all replies for this comment
```

### Recursively retrieving comments

[](#recursively-retrieving-comments)

You can retrieve all comments with their children and commenter from the commentable using the `commentsWithCommentsAndCommenter()` relation.

```
$post = Post::find(1);

$post->commentsWithCommentsAndCommenter
```

### Cascade on delete

[](#cascade-on-delete)

Deleting a commentable will also delete all comments that are attached to it.

### Using a custom Comment class

[](#using-a-custom-comment-class)

If you are using a custom comment class make sure it extends the default `Comment` class that is shipped with this package.

```
namespace App\Models;
use Rubik\LaravelComments\Models\Comment;

class CustomComment extends Comment
{
    ...
}
```

In addition to that, you need to set the `comment_model` value in the config file to the path of your custom class.

```
// config/comments.php

return [
     ...

    'comment_model' => App\Models\CustomComment::class,

     ...
]
```

```
$post->comment('My custom comment!');

$post->comments->first(); // will return an instance of CustomComment
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Rron Nela](https://github.com/rronik)
- [Yllndrit Beka](https://github.com/yllndritb)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1517d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a7c1f9aba4ff97c724276769d2df23b56a640cc2548340763355e65190bbf758?d=identicon)[rubik.llc.dev](/maintainers/rubik.llc.dev)

---

Top Contributors

[![rronik](https://avatars.githubusercontent.com/u/35034872?v=4)](https://github.com/rronik "rronik (16 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (11 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (10 commits)")

---

Tags

commentslaravelphplaravellaravel-commentsrubik-llc

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/rubik-llc-laravel-comments/health.svg)

```
[![Health](https://phpackages.com/badges/rubik-llc-laravel-comments/health.svg)](https://phpackages.com/packages/rubik-llc-laravel-comments)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
