Native comments for your Laravel application.

Overview

Comments

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

Become a Patron

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

  • View comments
  • Create comments
  • Delete comments
  • Edit comments
  • Reply to comments
  • Authorization rules
  • Support localization
  • Dispatch events
  • Route, Controller, Comment, Migration & View customizations
  • Support for non-integer IDs
  • Support for multiple User models
  • Solved N+1 query problem
  • Comment approval (opt-in)
  • Guest commenting (opt-in)
  • Pagination (opt-in)
  • Soft deletes (opt-in)
  • Works with custom ID columns
  • Optionally load package migrations [NEW]
  • Configure maximum indentation level [NEW]

Screenshots

Here are a few screenshots.

No comments & guest:

No comments & logged in:

One comment:

One comment edit form:

Two comments from different users:

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:

Installation

From the command line:

composer require laravelista/comments

Run migrations

We need to create the table for comments.

php artisan migrate

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 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 & configure (optional)

Publish the config file (optional):

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

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)

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

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

Publish translations (customization)

The package currently only supports English, but I am open to PRs for other languages.

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

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

To view only approved comments, use this code:

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

Paginate comments

Pagination paginates by top level comments only, meaning that if you specify the number of comments per page to be 1, and that one comment has 100 replies, it will display that one comment and all of its replies.

It was not possible to do it any other way, because if I paginate by all comments (parent and child) you will end up with blank pages since the comments components loops parent comments first and then uses recursion for replies.

To use pagination, use this code:

@comments([
    'model' => $user,
    'perPage' => 2
])

Replace 2 with any number you want.

Configure maximum indentation level

By default the replies go up to level three. After that they are "mashed" at that level.

- 0
    - 1
        - 2
            - 3

You can configure the maximum indentation level like so:

@comments([
    'model' => $user,
    'maxIndentationLevel' => 1
])

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

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

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

POST /comments

Request data:

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

PUT /comments/{comment}

  • {comment} - Comment ID.

Request data:

'message' => 'required|string'

POST /comments/{comment}

  • {comment} - Comment ID.

Request data:

'message' => 'required|string'

Upgrading from older versions (troubleshoot)

Before creating an issue, read this.

Sponsors & Backers

I would like to extend my thanks to the following sponsors & backers for funding my open-source journey. If you are interested in becoming a sponsor or backer, please visit the Backers page.

Contributing

Thank you for considering contributing to Comments! The contribution guide can be found Here.

Code of Conduct

In order to ensure that the open-source community is welcoming to all, please review and abide by the Code of Conduct.

License

Comments is open-source software licensed under the MIT license.

Comments
  • 422 error when trying to comment?

    422 error when trying to comment?

    Hi. I wanted to give your package a shot and implementation went smoothly until I tried to post a comment for the first time. It appears that three api calls happen, two of which are the same:

    one is a GET of /api/v1/comments, returns a 422. this is the response: {"error":{"message":{"content_type":["The content type field is required."],"content_id":["The content id field is required."]},"status_code":422}} It doesn't seem to send the contents of window.Laravelista, which I have to imagine is what it wants to do.

    Then a second one happens that seems marginally more successful. It is a GET of http://www.airhub.dev/api/v1/comments?content_type=App%5CModels%5CExpense&content_id=19, which returns something like this: {"data":[],"meta":{"user":{"data":{"id":2,"name":"User Name","email":"[email protected]"}}}}

    But the content I type in never seems to be posted.

    I'm trying it out with Chrome 57, Laravel 5.2, and PHP 7.

    If I had a non-minified copy of the React I would spend more time trying to suss out the issue, although my React skills are limited; since only the minified versions are in the repo, I'm just going to go ahead and ask for help.

    Thanks!

    opened by agouticreative 21
  • Laravel 9.x Compatibility

    Laravel 9.x Compatibility

    This is an automated pull request from Shift to update your package code and dependencies to be compatible with Laravel 9.x.

    Before merging, you need to:

    • Checkout the l9-compatibility branch
    • Review all comments for additional changes
    • Thoroughly test your package

    If you do find an issue, please report it by commenting on this PR to help improve future automation.

    opened by laravel-shift 16
  • Comment Post

    Comment Post

    Hi Mario, I hope you're well I face a really strange problem, I am using the comment system in my system and now I am implementing it in another location within the same system. The difference at this point is that I am implementing it within a modal window. When I click the submit button it stops on the route /comments with 404 error I have inserted a dd($request->all()) in the controller to check if the post is being sent and apparently everything is normal

    "_token" => "XYZ" "my_name_9fN8cdliU8hyNv5l" => null "valid_from" => "bla bla bla" "commentable_type" => "\App\Project" "commentable_id" => "26" "message" => "test message"

    Any idea ? Thanks in advanced

    opened by vitormicillo 13
  • How to make reply available only for admins?

    How to make reply available only for admins?

    Hi! this really help me finished what I'm working on, Thank you for this! Keep up the good work!

    I just want to know if there is an option that only type=admins can reply to a users comment.

    opened by julem-sen 12
  • Delete comments from other users

    Delete comments from other users

    Hi, I am new to Laravel and currently doing a school project. I've found your project on youtube from Andre and I have a problem with the comments of other users on the user's posts. I can reply to users without problem but if I add the same structure of post delete to the comments (with the condition if the post owner is connected and viewing the post) gives me a 403 error. I don't know if I need to some additional changes or something in the existing delete code. Thank you.

    question 
    opened by Helios14Max 11
  • This package causes App Engine deployments to fail.

    This package causes App Engine deployments to fail.

    Hello,

    This package is awesome. I set it up in a few minutes, adjusted it to my liking and then tried to deploy a test version to my appengine production project.

    As far as I can tell its happening during the composer scripts. As an example:

    Step #1 - "builder": In Compiler.php line 36:
    Step #1 - "builder": 
    Step #1 - "builder": Please provide a valid cache path. 
    
    ... then ...
    
    Step #1 - "builder": Generating optimized autoload files
    Step #1 - "builder": > Illuminate\Foundation\ComposerScripts::postAutoloadDump
    Step #1 - "builder": > @php artisan package:discover --ansi
    Step #1 - "builder": Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
    Step #1 - "builder": 
    Step #1 - "builder": error: `composer_install` returned code: 1
    Step #1 - "builder": Traceback (most recent call last):
    Step #1 - "builder": File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    Step #1 - "builder": "__main__", fname, loader, pkg_name)
    Step #1 - "builder": File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    Step #1 - "builder": exec code in run_globals
    Step #1 - "builder": File "/usr/local/bin/ftl.par/__main__.py", line 65, in <module>
    Step #1 - "builder": File "/usr/local/bin/ftl.par/__main__.py", line 57, in main
    Step #1 - "builder": File "/usr/local/bin/ftl.par/__main__/ftl/common/ftl_error.py", line 58, in UserErrorHandler
    Step #1 - "builder": IOError: [Errno 2] No such file or directory: '""/output'
    Finished Step #1 - "builder"
    ERROR
    ERROR: build step 1 "gcr.io/gae-runtimes/php72_app_builder:php72_20190515_7_2_17_RC00" failed: exit status 1
    

    Just wanted to know if you had any idea what could be causing this? I am using Laravel 5.7, php72 and if I composer remove your package, the deployment scripts run fine. My suspicion is that it is trying to cache or discover something in your package (or the included markdown parser) and failing.

    Keep in mind, I realize your package probably works on 99% of deployments... and appengine is a weird case. So I'm just looking for advice if you have time. The issue seems to happen at the same step as this previously closed ticket: https://github.com/laravelista/comments/issues/16

    Thanks!

    opened by flexgrip 11
  • Change Image

    Change Image

    The default image is showing with the comment. I have image path of user profile in column name avatar in user table. How can i set the image from that column

    enhancement 
    opened by aqiltariq 11
  • Comments are not displayed

    Comments are not displayed

    Hello Again!

    I am trying to include this package to my project and Initially I tried to do it by the book, that didn't work as it shows the error referenced in my last post here #144.

    Now I thought I solved it by instead of giving my Commentable model I gave the user instead,

    Now the commenting section is displayed and I can comment, when I submit a comment it comes to my database, but no comments are ever displayed, it's always showing the default "no comments yet" message.

    What should I do?

    opened by EricTalv 10
  • Using the Guard System

    Using the Guard System

    We're using the Laravel Guards system. Has anyone gotten this to work with this yet? We can't seem to get this one to work with this type of setup for commenters:

      'guards' => [
           'web' => [
               'driver' => 'session',
               'provider' => 'users',
           ],
    
           'api' => [
               'driver' => 'token',
               'provider' => 'users',
           ],
    
           'member' => [
               'driver' => 'session',
               'provider' => 'member',
           ],
       ],
    

    I've tried to use the following as well:

    'controller' => '\XXXXXXXX\Member\Http\Controllers\Comments',

    Which is:

    <?php
    
    namespace XXXXXXXXX\Member\Http\Controllers;
    
    use  Laravelista\Comments\CommentController;
    
    class Comments extends CommentController
    {
        public function __construct()
        {
            $this->middleware('member');
    
            if (config('comments.guest_commenting') == true) {
                $this->middleware('auth')->except('store');
            } else {
                $this->middleware('auth');
            }
        }
    }
    

    It seems it's not seeing my change to the config pointing at the new controller after a dump-autoload as well. So, my question, does this package support the guard system and us defining 'provider' => 'member', ?

    Because I'm not seeing it pick up the logged in member's session at all, where the member can view their dashboard and etc, but on this blog post, we're trying to get this to work there, and it still shows the login button and it's not seeing they're logged in...

    help wanted 
    opened by jimcouts 10
  • Guest commenting

    Guest commenting

    I would like users to be able to leave a comment as a guest (without logging in) on my project. Is there any support or roadmap for this? If not, would you be open to a PR? I would think requiring a recaptcha a for guest comments would be sensible.

    enhancement 
    opened by mattrabe 10
  • moveing package from local to sharing host

    moveing package from local to sharing host

    hi im using sharing host and im Inaccessibility to command line for composer . all files is copied to host like vendor folde and trait is added. but is still not working. what i do?

    opened by mahdi4k 9
  • markdown styling is not working

    markdown styling is not working

    package version: "laravelista/comments": "^4.5", framework version: "laravel/framework": "^8.83.23",

    I have an issue when I want to write emojis in comments, it shows as it is without the styling, but code blocks are working like here

    example
    

    and only this is working! any ideas about the solution?

    opened by NawrasBukhari 0
  • Section added after the comment block is not working -

    Section added after the comment block is not working -

    We are facing a strange issue, sections added after the following block is not rendering in the browser. We have added jQuery after this block and it's not rendering in the browser -

    @comments(['model' => $document])

    opened by maheshmbisen 0
  • Call to a member function count() on null

    Call to a member function count() on null

    Hi, ive encountered this error Error Call to a member function count() on null (View: C:\Users\hayNaku\Desktop\sample\skMalitbog\resources\views\vendor\comments\components\comments.blade.php)

    can you guide me with this, im new to laravel, im using laravel 8..Thnks in advance

    opened by galahan1987 3
  • Loading all comments?

    Loading all comments?

    So it seems that you are actually loading ALL the comments from the database before processing the pagination, what would happen in an environment when you have thousands of comments per item?

    https://github.com/laravelista/comments/blob/f1c7a476f79b80f0e28ba901436832f773299ee6/resources/views/components/comments.blade.php#L3

    opened by Yahav 1
  • unable to install in laravel 9

    unable to install in laravel 9

    Info from https://repo.packagist.org: #StandWithUkraine Using version ^4.5 for laravelista/comments ./composer.json has been updated Running composer update laravelista/comments Loading composer repositories with package information Updating dependencies Your requirements could not be resolved to an installable set of packages.

    Problem 1 - Root composer.json requires laravelista/comments ^4.5 -> satisfiable by laravelista/comments[4.5.0]. - laravelista/comments 4.5.0 requires illuminate/database ^8.0 -> found illuminate/database[v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.

    You can also try re-running composer require with an explicit version constraint, e.g. "composer require laravelista/comments:*" to figure out if any version is installable, or "composer require laravelista/comments:^2.1" if you know which you need.

    Installation failed, reverting ./composer.json and ./composer.lock to their original content.

    opened by Shubhamlmp 5
  • Spatie permission issues after upgrading to Laravel 9

    Spatie permission issues after upgrading to Laravel 9

    Hello, I don't certainly know if this is an issue of laravelista/comments. After upgrading to laravel 9 I could't create new comments since I was redirected to the login page of Jetstream. To fix the problem I had to move this routes (found in vendor/laravel/laravelista/comments/src/routes.php) to my web.php file:

    Route::post('comments', Config::get('comments.controller') . '@store')->name('comments.store'); Route::delete('comments/{comment}', Config::get('comments.controller') . '@destroy')->name('comments.destroy'); Route::put('comments/{comment}', Config::get('comments.controller') . '@update')->name('comments.update'); Route::post('comments/{comment}', Config::get('comments.controller') . '@reply')->name('comments.reply');

    Now is working correctly. Just to let you know. You can close the issue if is a problem of myself. Only thing I know is this wasn't happening after upgrade. Thanks for this awesome package

    opened by RobRover 2
Releases(4.5.0)
  • 4.5.0(Mar 15, 2021)

  • 4.3.0(Oct 30, 2020)

    Uses Bootstrap CSS instead of Tailwind CSS which is the default in Laravel 8. Fixes issue when having the component on a deeper URL would generate incorrect URLs.

    Source code(tar.gz)
    Source code(zip)
  • 4.2.0(Oct 30, 2020)

  • 4.1.0(Oct 29, 2020)

  • 4.0.0(Oct 29, 2020)

  • 3.5.0(Apr 19, 2020)

    I haven't tested it yet, so let me know if there are any errors so that I can fix them asap. I'm planning to implement workbench for testing so that I don't have to test everything by hand every time.

    Source code(tar.gz)
    Source code(zip)
  • 3.4.0(Apr 19, 2020)

  • 3.3.0(Feb 8, 2020)

  • 3.1.0(Sep 7, 2019)

    Changelog:

    • Added Spam protection when guest commenting is enabled @rocramer
    • Added Laravel 6 support @joelwmale
    • Added performance improvements with nested comments @guratr
    • Fixed guest validation rules @rocramer

    Thank you everyone for contributing :heart:

    Source code(tar.gz)
    Source code(zip)
  • 3.0.0(Jul 1, 2019)

    • non-integer IDs
    • multiple user models
    • custom Comment model use
    • solved N+1 query problem
    • comment approval feature
    • guest commenting feature

    Thanks to @eworwa for making this package support multiple user models and customization of the comment class.

    Thanks to @bosunski for enabling publishing of migrations.

    Source code(tar.gz)
    Source code(zip)
  • 2.1.0(Feb 18, 2019)

    Thanks to @macagoraga for making this happen.

    In this release it is possible to define your own custom permission for creating, editing, replying and deleting comments.

    Read more in this issue: https://github.com/laravelista/comments/issues/34

    Related PRs:

    • https://github.com/laravelista/comments/pull/38
    • https://github.com/laravelista/comments/pull/37
    • https://github.com/laravelista/comments/pull/36
    • https://github.com/laravelista/comments/pull/35
    Source code(tar.gz)
    Source code(zip)
  • 2.0.1(Jul 14, 2018)

    There was an issue preventing the migration to run. I had set the child_id column as big integer, but have left the id as just integer. This was preventing the migration to run on Mysql and Maria DB databases where foreign keys work.

    I was using sqlite for testing the package. My bad.

    You can read more about this issue here https://github.com/laravelista/comments/issues/15

    Thanks to @thekillerisme, @jeverington and @walidbagh.

    Source code(tar.gz)
    Source code(zip)
  • 2.0.0(Jul 1, 2018)

    New functionality:

    • it is possible now to reply to comments (nesting)
    • it is possible now to customize the UI (design & localization)
    • the default UI is now made with Bootstrap 4; no more JS ~~(React.js)~~
    • simpler installation
    • simpler usage
    Source code(tar.gz)
    Source code(zip)
  • 1.1.2(Sep 7, 2017)

  • 1.1.1(Apr 12, 2017)

    Be sure to run:

    php artisan vendor:publish --provider="Laravelista\Comments\Providers\CommentsServiceProvider" --tag=public --force
    

    after updating the package, to get the latest .js file.

    Big thanks to @agouticreative for detecting the issue and finding the solution for this. https://github.com/laravelista/comments/issues/6

    Source code(tar.gz)
    Source code(zip)
Owner
Laravelista
Specialized in building Web & Mobile Apps
Laravelista
Laravel-comments-livewire - Livewire components for the laravel-comments package

Associate comments and reactions with Eloquent models This package contains Livewire components to be used with the spatie/laravel-comments package. S

Spatie 15 Jan 18, 2022
A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

Laravel Moderation A simple Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc. Keep yo

Alex Kyriakidis 509 Dec 30, 2022
An index of Laravel's cascading comments

Cascading Comments An index of Laravel's cascading comments. Visit the site at cascading-comments.sjorso.com. About This is a cascading comment: casca

Sjors Ottjes 4 Apr 28, 2022
A dead-simple comments package for Laravel.

A dead-simple comments package for Laravel. This package provides an incredibly simple comment system for your Laravel applications. If you're looking

Ryan Chandler 120 Dec 4, 2022
Simplifies writing DocBlock comments in Javascript, PHP, CoffeeScript, Actionscript, C & C++

DocBlockr DocBlockr is a package for Sublime Text 2 & 3 which makes writing documentation a breeze. DocBlockr supports JavaScript (including ES6), PHP

Nick Fisher 3.1k Nov 25, 2022
Madison is a platform for lawmakers to share legislation with their citizens, allowing the community to add comments and suggest improvements.

Madison Madison is an open-source document engagement and feedback platform. While Madison can be used to collaborate on many different kinds of docum

OpenGov Foundation 591 Dec 17, 2022
Automatically load your helpers in your laravel application.

Laravel AutoHelpers Automatically load your helpers in your laravel application. Installation You can install the package via composer: composer requi

Florian Wartner 6 Jul 26, 2021
Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Laravel Segment Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application. Installation You can install the pac

Octohook 13 May 16, 2022
Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

null 686 Dec 28, 2022
Laravel Larex lets you translate your whole Laravel application from a single CSV file.

Laravel Larex Translate Laravel Apps from a CSV File Laravel Larex lets you translate your whole Laravel application from a single CSV file. You can i

Luca Patera 68 Dec 12, 2022
Laravel package to find performance bottlenecks in your laravel application.

Laravel Meter Laravel Meter monitors application performance for different things such as requests, commands, queries, events, etc and presents result

Sarfraz Ahmed 230 Dec 27, 2022
Laravel-tagmanager - An easier way to add Google Tag Manager to your Laravel application.

Laravel TagManager An easier way to add Google Tag Manager to your Laravel application. Including recommended GTM events support. Requirements Laravel

Label84 16 Nov 23, 2022
Laravel Logable is a simple way to log http request in your Laravel application.

Laravel Logable is a simple way to log http request in your Laravel application. Requirements php >= 7.4 Laravel version >= 6.0 Installation composer

Sagar 6 Aug 25, 2022
Catch incoming emails in your Laravel application

Laravel Mailbox ?? Handle incoming emails in your Laravel application. Mailbox::from('{username}@gmail.com', function (InboundEmail $email, $username)

Beyond Code 918 Jan 2, 2023
Deploy and execute non-PHP AWS Lambda functions from your Laravel application.

Sidecar for Laravel Deploy and execute non-PHP AWS Lambda functions from your Laravel application. Read the full docs at hammerstone.dev/sidecar/docs.

Hammerstone 624 Dec 30, 2022
Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.

About LivewireUI Spotlight LivewireUI Spotlight is a Livewire component that provides Spotlight/Alfred-like functionality to your Laravel application.

Livewire UI 792 Jan 3, 2023
A Laravel package helps you add a complete real-time messaging system to your new / existing application with only one command.

A Laravel package helps you add a complete real-time messaging system to your new / existing application with only one command.

Munaf Aqeel Mahdi 1.7k Jan 5, 2023
Laravel package that converts your application into a static HTML website

phpReel Static Laravel Package phpReel Static is a simple Laravel Package created and used by phpReel that converts your Laravel application to a stat

phpReel 16 Jul 7, 2022
Dashboard to view your http client requests in laravel application

Laravel Blanket is a package with wraps laravel http client requests and provide logs for request and response, also give option to retry any request from dashboard and more...

Ahmed waleed 215 Dec 29, 2022