nirajan / comment
A Laravel package for handling comments
Package info
Language:Blade
pkg:composer/nirajan/comment
Requires
- php: ^8.0
- illuminate/support: ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^10.0
README
A lightweight Laravel package for adding comments to any Eloquent model using polymorphic relationships.
Features
- Polymorphic comment support for any Eloquent model
- Blade components for comment forms and comment lists
- Admin dashboard for moderation, approval, and deletion
- Optional ReCAPTCHA validation support
- Configurable middleware, URL prefix, layout, and approval workflow
- Easy installation and package reuse
Requirements
- PHP 8.1+
- Laravel 10.x, 11.x, or 12.x
Installation
- Install the package via Composer:
composer require nirajan/comment
- Publish package resources:
php artisan vendor:publish --provider="Nirajan\Comment\CommentServiceProvider"
- Run the migrations:
php artisan migrate
Configuration
The package publishes a configuration file at config/comment.php.
Example configuration:
return [ 'admin_middleware' => ['web', 'admin'], 'recaptcha_site_key' => env('RECAPTCHA_SITE_KEY'), 'recaptcha_secret_key'=> env('RECAPTCHA_SECRET_KEY'), 'admin_url_prefix' => 'AdminActivity', 'admin_layout' => 'backend.layouts.master', 'need_approval' => false, 'post_route' => [ 'name' => 'posts.detail', 'param' => 'id', 'callback' => null, ], 'theme' => [ 'icon_button_color' => '#fd0000', 'icon_button_color_secondary' => '#fd0000', ], ];
Important configuration options
admin_middleware- Middleware applied to the admin comment routesadmin_url_prefix- URL prefix used for the dashboard routeadmin_layout- Blade layout used by admin viewsneed_approval- Set totrueto require manual approval before comments are publicpost_route- Redirect options for a comment reply route or detail route
Usage
Enable comments on a model
Add the HasComments trait to any model:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Nirajan\Comment\Traits\HasComments; class Post extends Model { use HasComments; }
Display the comment form and list
Use the provided Blade components in your view:
<x-comment::comment-form :model="$post" /> <x-comment::comment-list :model="$post" />
Component props
comment-formexpects amodelinstance that usesHasCommentscomment-listexpects amodelinstance and displays related comments
Admin Dashboard
Visit the admin dashboard at:
/ AdminActivity/comments
The dashboard supports:
- viewing all comments
- approving or rejecting comments
- deleting comments
- filtering by approval status
Update
admin_url_prefixandadmin_layoutinconfig/comment.phpto match your application.
ReCAPTCHA Support
If you want to enable ReCAPTCHA validation, add your keys to .env:
RECAPTCHA_SITE_KEY=your-site-key RECAPTCHA_SECRET_KEY=your-secret-key
Then update config/comment.php accordingly.
Customization
You can customize package behavior through config/comment.php and by overriding published views in your application if needed.
License
This package is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions, issues, and pull requests are welcome. Please open an issue first if you want to propose a larger change.