›
›
›
  1. docs
  2. ›
  3. byrcsc/laravel-comments
1.x
Browse documentationOpenClose

Getting started

  • Introduction
  • Installation and setup
  • Quick start

Core concepts

  • Commentable models
  • Threads and replies
  • Moderation
  • Initial status
  • Pinning
  • Reactions
  • Edits and revisions
  • Attachments
  • Deleting comments

Operations

  • Comment counts
  • Events and listeners
  • Reply notifications
  • Authorization
  • Rendering and safety

Reference

  • Configuration
  • Console commands
  • Testing
  • Troubleshooting

Getting started

  • Introduction
  • Installation and setup
  • Quick start

Core concepts

  • Commentable models
  • Threads and replies
  • Moderation
  • Initial status
  • Pinning
  • Reactions
  • Edits and revisions
  • Attachments
  • Deleting comments

Operations

  • Comment counts
  • Events and listeners
  • Reply notifications
  • Authorization
  • Rendering and safety

Reference

  • Configuration
  • Console commands
  • Testing
  • Troubleshooting

byrcsc/laravel-comments · 1.x

Quick start.

Add comments, replies, moderation, and safe rendering to an Eloquent model.

This walkthrough adds a moderated comment thread to a Post. It assumes Laravel Comments is installed.

1. Make the model commentable

Add the HasComments trait:

use ByRcsc\LaravelComments\Concerns\HasComments;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasComments;
}

The model now has a comments() relationship and methods for authenticated and guest comments.

2. Write a comment and reply

$comment = $post->comment('Great write-up!', by: $user);

$reply = $comment->reply(
    'Agreed, especially the last section.',
    by: $teammate,
);

The by argument accepts any saved Eloquent model. Laravel Comments stores the actor through a polymorphic relationship and does not require App\Models\User.

Replies remain attached to the same commentable model as their parent:

$reply->parent_id; // the first comment's key
$reply->depth();   // 1

3. Moderate a guest comment

$guest = $post->commentAsGuest(
    'Where can I download the slides?',
    name: 'Jane',
    email: 'jane@example.com',
);

$guest->status; // CommentStatus::Pending

$guest->approve(by: $moderator);

Guest comments always start as pending. Approving changes the stored status and dispatches a moderation event.

Status does not control visibility by itself. Your query decides which comments appear to visitors.

4. Read and render the thread

Load approved top-level comments and their direct replies:

$threads = $post->comments()
    ->approved()
    ->topLevel()
    ->with('replies')
    ->get();

Render comment bodies with escaped Blade output:

{{ $comment->body }}

Laravel Comments stores bodies and guest details exactly as received. Sanitize the value before rendering it as Markdown or HTML.

What to read next

  • Threads and replies for deeper nesting and depth limits.
  • Moderation for every status, transition, and query scope.
  • Reactions to add identity-based reactions to a comment.
  • Authorization to protect write and moderation actions.
  • Rendering and safety to handle untrusted content and attachment URLs.
PreviousInstallation and setupNextCommentable models
View source

On this page

  1. 1. Make the model commentable
  2. 2. Write a comment and reply
  3. 3. Moderate a guest comment
  4. 4. Read and render the thread
  5. What to read next