Simple Laravel comment system
Simple comments system, loosely inspired by Django's comments system. Allows comments to be attached to an object, and allows for them to be flagged by users for moderator attention.
composer require matthewbdaly/laravel-comments
You will also need to run
php artisan migrate to set up the required database tables.
To allow comments to be attached to an object, add the trait
Matthewbdaly\LaravelComments\Eloquent\Traits\Commentable to it. The object will then have a
comments polymorphic relation.
The package includes a view for submitting comments, named
comments::comments, which can be included in another view as follows:
@include('comments::comments', ['parent' => $post])
The value of
parent must be an instance of the commentable object (eg, a blog post). The view also includes a form for flagging comments.
Obviously, if you prefer you can override this view with your own content.
The package also includes the following views:
These simply acknowledge receipt of the comment or flag, but are fairly basic and so you should feel free to replace them as you see fit.
Of course there's nothing stopping you creating your own routes and controllers for creating, viewing and flagging comments, and if you wish to build a REST API that allows for adding comments to objects you can just use these models directly:
I recommend that you use my repositories, which are as follows:
matthewbdaly/laravel-repositories and so implement caching on the decorated repository, making it simple to ensure your models get cached appropriately.
You can set up listeners for the following events:
Fired before the comment is saved. The package does not validate comments out of the box, but you can use this to validate the comment using whatever method you wish (eg check it with Akismet, check for links), or process it before saving (eg run it through a Markdown parser).
Fired when a new comment is submitted. This is useful for creating some sort of notification, such as an SMS, email, push notification etc.
Fired before a comment flag is saved. Useful for processing the flag to amend it before saving it.
This event fires after a comment has been flagged for moderator attention. You can use this event to send whatever notification is most appropriate (eg, email, Slack, SMS).