Weave 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

Comments For Laravel Laravel Package

roundly-consulting/comments-for-laravel

A drop-in comments system for Laravel. Add threaded, polymorphic comments to any Eloquent model with migrations, models, and a simple API, no third-party service required. Configurable and lightweight, it's ready to integrate into your app in minutes.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package, publish its config and migrations, and run php artisan migrate.
  2. Add the package's commentable trait to every model that should accept comments (e.g. Post).
  3. Create a comment and return it through the package resource from a controller:
    return new CommentResource($post->comments()->create([...]));
    
  4. Look first at config/comments.php, specifically comments.resources.author. That key controls the JSON shape of every comment's author and is the main extension point.
  5. First use case: a GET /posts/{post}/comments endpoint returning a paginated CommentResource::collection(). The default payload is {id, ..., author: {id, type: "User"}} — enough for a frontend to render a comment list and link the author by ID.

Implementation Patterns

  • Standard endpoint shape. Return CommentResource::collection($comments) from index/show controllers. Paginate at the query level so the resource stays a thin serializer, and eager-load the author relation to avoid N+1s.
  • Nested resources. Embed comments inside a PostResource:
    'comments' => CommentResource::collection($this->whenLoaded('comments')),
    
    Consistent use of whenLoaded keeps payloads predictable when comments aren't requested.
  • Custom author payloads. Create App\Http\Resources\CommentAuthorResource extends JsonResource, add name, avatar_url, etc., and point comments.resources.author at it. The resource receives the author model, so $this->resource is your User (or other author model).
  • Keep the contract stable. Whatever you add to the author resource, retain id and type. Frontends and mobile clients should switch on type to choose a renderer, so treat it as an API constant rather than an implementation detail.
  • Multiple author types. If posts and pages can both be commented on, the basename type gives the client a discriminator (User, Page). Decide early whether that is your public contract or whether a custom resource should map to explicit strings.
  • Per-context author shapes. The config key is global. For a genuinely different shape on one endpoint, either swap the config in a middleware/service provider, or compose the author manually in a dedicated resource rather than bending the global setting.
  • Testing. Use model factories, hit the endpoint, and assert the contract: assertJsonPath('data.0.author.type', 'User') plus any custom fields. Add a test that swaps comments.resources.author to your custom class so config mistakes fail in CI, not production.

Gotchas and Tips

  • 1.2.0 changes the author type contract. author.type is now the author class basename (User), never the stored actor_type, full class name, or morph alias. Clients that parsed App\Models\User must be updated — this is a breaking change in payload values even though it fixes a class-name leak.
  • Morph maps no longer affect author.type. If you configured a morph alias like user and relied on it appearing in JSON, it now renders User. Restore the old value deliberately via a custom author resource if clients depend on it.
  • Basename collisions are real. App\Models\User and Admin\Models\User both serialize as User. If you support multiple author namespaces, map them to distinct type strings in your custom resource.
  • Config errors surface at render time, not boot time. A class that isn't an instantiable JsonResource subclass throws InvalidConfigurationException naming the key only when a comment is serialized. Run php artisan config:clear after editing, especially with cached config.
  • Custom resource requirements. It must be a concrete, instantiable JsonResource subclass; abstract classes, interfaces, or strings requiring constructor arguments won't work.
  • Guard null authors. A soft-deleted or absent author can leave you with nothing to render. Handle $this->resource being null in custom resources instead of assuming a model.
  • Watch query counts. The author resource touches the related model on every comment; eager-load the author relation and check with DB::listen or Telescope when adding fields.
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.
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator