Stars: 671
Forks: 140
Pull Requests: 42
Issues: 156
Watchers: 14
Last Updated: 2023-06-01 10:25:58
Native comments for your Laravel application.
License: MIT License
Languages: PHP, Blade
Comments is a Laravel package. With it you can easily implement native comments for your application.
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.
Here are a few screenshots.
No comments & guest:
No comments & logged in:
One comment:
One comment edit form:
Two comments from different users:
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:
From the command line:
composer require laravelista/comments
We need to create the table for comments.
php artisan migrate
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 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 the config file (optional):
php artisan vendor:publish --provider="Laravelista\Comments\ServiceProvider" --tag=config
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
You can publish migration to allow you to have more control over your table
php artisan vendor:publish --provider="Laravelista\Comments\ServiceProvider" --tag=migrations
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
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.
To view only approved comments, use this code:
@comments([
'model' => $book,
'approved' => true
])
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.
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
])
This package fires events to let you know when things happen.
Laravelista\Comments\Events\CommentCreated
Laravelista\Comments\Events\CommentUpdated
Laravelista\Comments\Events\CommentDeleted
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');
/comments
Request data:
'commentable_type' => 'required|string',
'commentable_id' => 'required|string|min:1',
'message' => 'required|string'
/comments/{comment}
Request data:
'message' => 'required|string'
/comments/{comment}
Request data:
'message' => 'required|string'
Before creating an issue, read this.
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.
Thank you for considering contributing to Comments! The contribution guide can be found Here.
In order to ensure that the open-source community is welcoming to all, please review and abide by the Code of Conduct.
Comments is open-source software licensed under the MIT license.