PHPackages                             softtechnoes/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. softtechnoes/comments

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

softtechnoes/comments
=====================

Comments for Laravel.

3.1.1(6y ago)017MITBladePHP ^7.1.3

Since Sep 29Pushed 5y agoCompare

[ Source](https://github.com/softtechnoes/soft_comments)[ Packagist](https://packagist.org/packages/softtechnoes/comments)[ Patreon](https://www.patreon.com/laravelista)[ RSS](/packages/softtechnoes-comments/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (5)Versions (15)Used By (0)

Comments
========

[](#comments)

Comments is a Laravel package. With it you can easily implement native comments for your application.

[![Become a Patron](https://camo.githubusercontent.com/16fde1abb7601eba23de38f592bc54e3a7d10da24ac58db594d7fab32d449d46/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4265636f6d65253230612d506174726f6e2d6639363835342e7376673f7374796c653d666f722d7468652d6261646765)](https://www.patreon.com/laravelista)

Overview
--------

[](#overview)

This package can be used to comment on any model you have in your application.

All comments are stored in a single table with a polymorphic relation for content and a polymorphic relation for the user who posted the comment.

### Features

[](#features)

- View comments
- Create comments
- Delete comments
- Edit comments
- **Reply to comments**
- **Authorization rules**
- **Support localization**
- **Dispatch events**
- **Route, Controller, Comment, Migration &amp; View customizations**
- **Support for non-integer IDs**
- **Support for multiple User models**
- **Solved N+1 query problem**
- **Comment approval (opt-in)**
- **Guest commenting**

### Screenshots

[](#screenshots)

Here are a few screenshots.

No comments &amp; guest:

[![](https://camo.githubusercontent.com/264e6915c0bb966e251c87be7fa5f4ad3247db56ea910c4e3f2ec2df13411018/68747470733a2f2f692e696d6775722e636f6d2f3964663458756e2e706e67)](https://camo.githubusercontent.com/264e6915c0bb966e251c87be7fa5f4ad3247db56ea910c4e3f2ec2df13411018/68747470733a2f2f692e696d6775722e636f6d2f3964663458756e2e706e67)

No comments &amp; logged in:

[![](https://camo.githubusercontent.com/0784c78a8430f2b6da145921197ed49726e7141244e42e57a0d4bd2e9202e55d/68747470733a2f2f692e696d6775722e636f6d2f414c49364762522e706e67)](https://camo.githubusercontent.com/0784c78a8430f2b6da145921197ed49726e7141244e42e57a0d4bd2e9202e55d/68747470733a2f2f692e696d6775722e636f6d2f414c49364762522e706e67)

One comment:

[![](https://camo.githubusercontent.com/925fcf402a670fc73a8bdcb6be8ab05ba8270afa272acbacb03b533e5c04d748/68747470733a2f2f692e696d6775722e636f6d2f3977424e6979322e706e67)](https://camo.githubusercontent.com/925fcf402a670fc73a8bdcb6be8ab05ba8270afa272acbacb03b533e5c04d748/68747470733a2f2f692e696d6775722e636f6d2f3977424e6979322e706e67)

One comment edit form:

[![](https://camo.githubusercontent.com/ec9aafbae41358688d2fdecc5b12f4a42c1e900f0384c4aa52e1fd8a396e0128/68747470733a2f2f692e696d6775722e636f6d2f6378446833344f2e706e67)](https://camo.githubusercontent.com/ec9aafbae41358688d2fdecc5b12f4a42c1e900f0384c4aa52e1fd8a396e0128/68747470733a2f2f692e696d6775722e636f6d2f6378446833344f2e706e67)

Two comments from different users:

[![](https://camo.githubusercontent.com/4d4650eb726f4c0571931e080798c6112b956944fc97184610dadaa8072d5fa0/68747470733a2f2f692e696d6775722e636f6d2f325035753235782e706e67)](https://camo.githubusercontent.com/4d4650eb726f4c0571931e080798c6112b956944fc97184610dadaa8072d5fa0/68747470733a2f2f692e696d6775722e636f6d2f325035753235782e706e67)

### Tutorials &amp; articles

[](#tutorials--articles)

I plan to expand this chapter with more tutorials and articles. If you write something about this package let me know, so that I can update this chapter.

**Screencasts:**

- [Adding comments to your Laravel application](https://www.youtube.com/watch?v=YhA0CSX1HIg) by Andre Madarang.

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

[](#installation)

From the command line:

```
composer require laravelista/comments
```

### Run migrations

[](#run-migrations)

We need to create the table for comments.

```
php artisan migrate
```

### Add Commenter trait to your User model

[](#add-commenter-trait-to-your-user-model)

Add the `Commenter` trait to your User model so that you can retrieve the comments for a user:

```
use Laravelista\Comments\Commenter;

class User extends Authenticatable
{
    use Notifiable, Commenter;
}
```

### Add Commentable trait to models

[](#add-commentable-trait-to-models)

Add the `Commentable` trait to the model for which you want to enable comments for:

```
use Laravelista\Comments\Commentable;

class Product extends Model
{
    use Commentable;
}
```

### Publish Config &amp; configure (optional)

[](#publish-config--configure-optional)

Publish the config file (optional):

```
php artisan vendor:publish --provider="Laravelista\Comments\ServiceProvider" --tag=config
```

### Publish views (customization)

[](#publish-views-customization)

The default UI is made for Bootstrap 4, but you can change it however you want.

```
php artisan vendor:publish --provider="Laravelista\Comments\ServiceProvider" --tag=views
```

### Publish Migrations (customization)

[](#publish-migrations-customization)

You can publish migration to allow you to have more control over your table

```
php artisan vendor:publish --provider="Laravelista\Comments\ServiceProvider" --tag=migrations
```

Usage
-----

[](#usage)

In the view where you want to display comments, place this code and modify it:

```
@comments(['model' => $book])

```

In the example above we are setting the `commentable_type` to the class of the book. We are also passing the `commentable_id` the `id` of the book so that we know to which book the comments relate to. Behind the scenes, the package detects the currently logged in user if any.

If you open the page containing the view where you have placed the above code, you should see a working comments form.

### View only approved comments

[](#view-only-approved-comments)

To view only approved comments, use this code:

```
@comments([
    'model' => $book,
    'approved' => true
])

```

Events
------

[](#events)

This package fires events to let you know when things happen.

- `Laravelista\Comments\Events\CommentCreated`
- `Laravelista\Comments\Events\CommentUpdated`
- `Laravelista\Comments\Events\CommentDeleted`

REST API
--------

[](#rest-api)

To change the controller or the routes, see the config.

```
Route::post('comments', '\Laravelista\Comments\CommentController@store');
Route::delete('comments/{comment}', '\Laravelista\Comments\CommentController@destroy');
Route::put('comments/{comment}', '\Laravelista\Comments\CommentController@update');
Route::post('comments/{comment}', '\Laravelista\Comments\CommentController@reply');

```

### POST `/comments`

[](#post-comments)

Request data:

```
'commentable_type' => 'required|string',
'commentable_id' => 'required|string|min:1',
'message' => 'required|string'

```

### PUT `/comments/{comment}`

[](#put-commentscomment)

- {comment} - Comment ID.

Request data:

```
'message' => 'required|string'

```

### POST `/comments/{comment}`

[](#post-commentscomment)

- {comment} - Comment ID.

Request data:

```
'message' => 'required|string'

```

Updating from older versions
----------------------------

[](#updating-from-older-versions)

### Support for guest commenting

[](#support-for-guest-commenting)

If you are updating an already existing database table `comments` and want support for guest commenting **(new installations get this by default)**, then create a new migration with `php artisan make:migration add_guest_commenting_columns_to_comments_table` and paste this code inside:

```
