Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Commentify Laravel Package

usamamuneerchaudhary/commentify

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require usamamuneerchaudhary/commentify
    php artisan vendor:publish --provider="UsamaMuneerChaudhary\Commentify\CommentifyServiceProvider" --tag="migrations"
    php artisan migrate
    
  2. Publish Config:

    php artisan vendor:publish --provider="UsamaMuneerChaudhary\Commentify\CommentifyServiceProvider" --tag="config"
    
    • Review config/commentify.php for default settings (e.g., commentable models, UI preferences).
  3. First Use Case:

    • For a Blog Post Model:
      // In Post model
      use UsamaMuneerChaudhary\Commentify\Traits\Commentable;
      
      class Post extends Model
      {
          use Commentable;
      }
      
    • Display Comments in Blade:
      @commentify('App\Models\Post', $post->id)
      
    • Trigger Livewire Component:
      <livewire:commentify />
      

Implementation Patterns

Core Workflows

  1. Model Integration:

    • Trait-Based: Attach use Commentable to any model to enable commenting.
    • Customization: Override default behavior via commentify() method in your model:
      public function commentify()
      {
          return $this->morphTo();
      }
      
  2. Livewire Component:

    • Default Usage: Embed <livewire:commentify /> in your Blade template.
    • Dynamic Context: Pass the commentable model and ID via props:
      <livewire:commentify :model="$post" :commentableType="App\Models\Post" />
      
  3. UI Customization:

    • Tailwind Overrides: Extend resources/css/commentify.css (published by the package).
    • Slot-Based Layouts: Use Blade slots for custom comment templates:
      @commentify('App\Models\Post', $post->id)
          @slot('comment', $comment)
              <div class="custom-comment">
                  {{ $comment->body }}
              </div>
          @endslot
      @endcommentify
      
  4. Validation & Rules:

    • Default Rules: Extend CommentifyRules in AppServiceProvider:
      use UsamaMuneerChaudhary\Commentify\Rules\CommentifyRules;
      
      CommentifyRules::extend(function ($rule, $attribute, $value, $fail) {
          if (strlen($value) < 10) {
              $fail('Comments must be at least 10 characters.');
          }
      });
      
  5. Nested Comments:

    • Enable via config:
      'nested_comments' => true,
      
    • Thread replies using the same component:
      <livewire:commentify :parentId="$comment->id" />
      

Gotchas and Tips

Pitfalls

  1. Model Morphing:

    • Issue: Forgetting to define commentify() in custom models can break polymorphic relationships.
    • Fix: Ensure the method returns morphTo() or a custom relationship:
      public function commentify()
      {
          return $this->morphTo();
      }
      
  2. Livewire Key Conflicts:

    • Issue: Multiple <livewire:commentify /> components on the same page may cause hydration errors.
    • Fix: Use unique keys:
      <livewire:commentify key="comments-{{ $post->id }}" />
      
  3. Tailwind Class Collisions:

    • Issue: Custom CSS may override package styles (e.g., .comment-body).
    • Fix: Use !important sparingly; prefer scoped classes:
      .commentify .custom-comment {
          @apply bg-gray-100;
      }
      
  4. Rate Limiting:

    • Issue: Spam comments if not configured.
    • Fix: Enable Laravel’s throttle middleware or use CommentifyMiddleware:
      'middleware' => [
          \UsamaMuneerChaudhary\Commentify\Http\Middleware\CommentifyMiddleware::class,
      ],
      

Debugging Tips

  1. Log Comment Events:

    • Enable debug mode in config:
      'debug' => env('COMMENTIFY_DEBUG', false),
      
    • Check storage/logs/laravel.log for Livewire/Commentify events.
  2. Wire:ignore for Static Content:

    • Wrap non-reactive elements to avoid unnecessary re-renders:
      @wire:ignore
          <div class="static-content">...</div>
      @endwire
      
  3. Database Seeding:

    • Use the CommentifySeeder for testing:
      php artisan db:seed --class=CommentifySeeder
      

Extension Points

  1. Custom Comment Models:

    • Extend the Comment model:
      class CustomComment extends \UsamaMuneerChaudhary\Commentify\Models\Comment
      {
          protected $table = 'custom_comments';
      }
      
    • Update config:
      'model' => \App\Models\CustomComment::class,
      
  2. Event Listeners:

    • Listen to comment events (e.g., CommentCreated):
      use UsamaMuneerChaudhary\Commentify\Events\CommentCreated;
      
      CommentCreated::listen(function ($comment) {
          // Send notification, log, etc.
      });
      
  3. API Endpoints:

    • Use the CommentifyController as a base for custom API routes:
      Route::post('/comments', [\UsamaMuneerChaudhary\Commentify\Http\Controllers\CommentifyController::class, 'store']);
      
  4. Localization:

    • Publish language files:
      php artisan vendor:publish --provider="UsamaMuneerChaudhary\Commentify\CommentifyServiceProvider" --tag="lang"
      
    • Override translations in resources/lang/en/commentify.php.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle