vigstudio/livewire-comments
Livewire-powered comment system for Laravel: attach comments to any model, run multiple comment widgets per page, support multiple auth guards, file/image uploads (drag & drop/paste), reCAPTCHA v3, emojis, Markdown, NSFW image checks, plus Echo updates.
Install the Package Run:
composer require vigstudio/livewire-comments
Publish Assets and Config Publish the required assets and configuration:
php artisan vendor:publish --tag=vgcomment-assets-livewire
php artisan vendor:publish --tag=vgcomment-config
Configure the Package
Edit config/vgcomment.php to customize:
vgcomment).mysql).vgcomments, vgcomment_files).name, email).web => [1] for admin IDs).Run Migrations Create the necessary database tables:
php artisan migrate
Enable Comments on a Model
Use the HasComments trait in your Eloquent model (e.g., Post.php):
use Vigstudio\LivewireComments\Traits\HasComments;
class Post extends Model
{
use HasComments;
}
Add the Livewire Component Include the comment component in your Blade view:
@livewire('vgcomment::comments', ['model' => $post], key($post->id))
Clear Caches Ensure changes take effect:
php artisan optimize:clear
Model Setup
Extend your Post model with HasComments:
class Post extends Model
{
use HasComments;
}
View Integration
Add the Livewire component to your post view (e.g., resources/views/posts/show.blade.php):
<div class="mt-8">
@livewire('vgcomment::comments', ['model' => $post], key($post->id))
</div>
Test the Flow
HasComments trait on any Eloquent model to enable comments.class Article extends Model
{
use HasComments;
}
$model->comments(): Access comments for the model.$model->addComment($content, $userId): Programmatically add a comment.$model->deleteComment($commentId): Delete a comment (if moderator).php artisan vendor:publish --tag=vgcomment-views
resources/views/vendor/vgcomment/.namespace App\Http\Livewire;
use Vigstudio\LivewireComments\Http\Livewire\Comments;
class CustomComments extends Comments
{
public function mount($model)
{
parent::mount($model);
// Custom logic (e.g., pre-fill comment text)
}
}
@livewire('custom-comments', ['model' => $post], key($post->id))
config/vgcomment.php:
'moderation_users' => [
'web' => [1, 2], // Admin IDs for web guard
'api' => [3, 4], // Admin IDs for API guard
],
FILESYSTEM_DISK in .env points to a writable disk (e.g., public or s3).config/vgcomment.php (if needed; defaults are provided).CommentAdded) in the Comments Livewire class:
public function addComment()
{
$this->validate([...]);
$comment = $this->model->comments()->create([...]);
$this->emit('commentAdded', $comment);
}
resources/views/vendor/vgcomment/.useMarkdown property.comments (main comments data).files (attachments).reactions (likes/reactions).reports (user-reported comments).settings (package configurations).php artisan vendor:publish --tag=vgcomment-migrations to inspect migrations before running migrate.public/vendor/vgcomment/.php artisan vendor:publish --tag=vgcomment-lang
resources/lang/vendor/vgcomment/ to add translations.config/vgcomment.php or disable it by setting 'nsfw_check' => false.anonymous users are handled in your auth system.GuestComment model if needed.use Livewire\Livewire;
public function test_comment_submission()
{
Livewire::test('vgcomment::comments', ['model' => $post])
->set('content', 'Test comment')
->call('addComment')
->assertSee('Test comment');
}
Livewire::test() to simulate user interactions (e.g., file uploads, emoji insertion).key() for multiple comment components on a page can cause Livewire to reuse state unexpectedly.@livewire('vgcomment::comments', ['model' => $post], key('post-'.$post->id))
FILESYSTEM_DISK in .env is misconfigured, uploads will fail silently.storage/logs/laravel.log for FileNotFoundException or PermissionDeniedException.php artisan storage:link
recaptcha is enabled but not configured, the package will throw validation errors..env:
VGCOMMENT_RECAPTCHA_SITE_KEY=your_site_key
VGCOMMENT_RECAPTCHA_SECRET_KEY=your_secret_key
How can I help you explore Laravel packages today?