beyondcode/laravel-comments

repository·master·Indexed 20 days ago

https://github.com/beyondcode/laravel-comments

A Laravel package for adding, managing, and nesting comments on Eloquent models. It provides the HasComments trait for model registration, support for nested replies, and a Commentator interface for managing comment approval logic. The package includes built-in events for comment creation and deletion, as well as configuration for cascading deletions of nested replies.

Tokens
2K
Snippets
14
Records
17
Agent score
69%

What's inside laravel-comments

  1. Nest comments (replies)

    master

    Since the BeyondCode\Comments\Comment class itself implements the HasComments trait, you can create replies by calling comment methods on an existing comment instance.

    $comment = BeyondCode\Comments\Comment::first();
    $comment->commentAsUser($user, "Hey there!");
  2. Register models to be commentable

    master

    To enable comment functionality on an Eloquent model, add the BeyondCode\Comments\Traits\HasComments trait to your model class.

    namespace App\Models;
    
    use Illuminate\
    Database\Eloquent\Model;
    use BeyondCode\Comments\Traits\HasComments;
    
    class Post extends Model
    {
        use HasComments;
        ...
    }
  3. Publish migrations and configuration

    master

    After installation, you must publish the migrations to create the necessary database tables and publish the configuration file to customize behavior.

    1. Publish migrations:
    php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="migrations"
    1. Run migrations:
    php artisan migrate
    1. Publish config:
    php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="config"
    php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="migrations"
    php artisan migrate
    php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="config"
  4. Auto-approve comments via the Commentator interface

    master

    You can control whether comments need approval by implementing the BeyondCode\Comments\Contracts\Commentator interface on your User model. Implement the needsCommentApproval($model) method to return logic for approval:

    • Return true: The comment will be marked as not approved.
    • Return false: The comment will be marked as approved.
    namespace App\Models;
    
    use BeyondCode\Comments\Contracts\Commentator;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class User extends Authenticatable implements Commentator
    {
        /**
         * Check if a comment for a specific model needs to be approved.
         * @param mixed $model
         * @return bool
         */
        public function needsCommentApproval($model): bool
        {
            return false; // Returns false to auto-approve
        }
    }
  5. Retrieve comments from models

    master

    Models using the HasComments trait can access their comments via the comments relationship. You can use the approved() scope to filter for only approved comments.

    $post = Post::find(1);
    
    // Retrieve all comments
    $comments = $post->comments;
    
    // Retrieve only approved comments
    $approved = $post->comments()->approved()->get();
  6. Create comments on models

    master

    Use the comment method to create a comment from the current context, or commentAsUser to associate a comment with a specific user model.

    • comment(string $content): Creates a comment with the provided text. Returns the newly created Comment instance.
    • commentAsUser(Model $user, string $content): Creates a comment associated with the provided user model.
    $post = Post::find(1);
    
    // Create a standard comment
    $comment = $post->comment('This is a comment from a user.');
    
    // Create a comment on behalf of a specific user
    $comment = $post->commentAsUser($user, 'This is a comment from someone else.');
  7. Configure comment deletion behavior

    master
    The package can automatically delete nested replies when a parent comment is deleted. This behavior is controlled by the comments.delete_replies_along_comments configuration key. If set to true, deleting a comment will trigger a cascade delete on its child comments.