Skip to content

Implementing a Comment system for Laravel's Eloquent models.

License

Notifications You must be signed in to change notification settings

AliBayat/Laravel-Commentable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Commentable Package

This Package makes it easy to implement Commenting system for Eloquent's Models. just use the trait in the model and you're good to go.

Requirements

  • PHP 7.2+
  • Laravel 7+

Installation

composer require alibayat/laravel-commentable

Publish and Run the migrations

php artisan vendor:publish --provider="AliBayat\LaravelCommentable\CommentableServiceProvider"
php artisan migrate

Laravel Commentable package will be auto-discovered by Laravel. and if not: register the package in config/app.php providers array manually.

'providers' => [
	...
	\AliBayat\LaravelCommentable\CommentableServiceProvider::class,
],

Setup models - just use the Trait in the Model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use AliBayat\LaravelCommentable\Commentable;

class Post extends Model
{
	use Commentable;

}

Usage

use App\Models\User;
use App\Models\Post;
use AliBayat\LaravelCommentable\Comment;


//  assuming that we have these variables
$user = User::first();
$post = Post::first();

Create a comment for the post

$commentData = [
	'title' => 'comment title (nullable)',
	'body' => 'comment body'
];

$post->comment($commentData, $user);

Create a child comment for the post

$parentComment = Comment::first();

$childCommentData = [
	'title' => 'comment title (nullable)',
	'body' => 'comment body'
];

$post->comment($childCommentData, $user, $parentComment);

Update a comment of the post

$comment = Comment::first();

$newData = [
	'body' => 'new body of the comment to update'
];

$post->updateComment($comment->id, $newData);

Delete a single comment of the post

$comment = Comment::first();

$post->deleteComment($comment->id);

Delete all the comments of the post

$post->comments()->delete();

Check if a comment has any children (boolean)

$comment = Comment::first();

$comment->hasChildren();

Count comments of the post

$post->commentCount();

Show comments on a post

$post->allComments(); // shows all comments (including children)
$post->comments(); // shows only top level comments

Activation

by default when you create a comment, it will be stored as a deactivated comment, unless you provide an 'active' field and set it to true:

$activeComment = [
	'body'   => 'comment body',
	'active' => true
];

$comment = $post->comment($activeComment, $user);

but you can always change the comment's state of activation by using below methods:

Activate

$comment->active();

// returns a boolean indicating the state of operation

Deactivate

$comment->deactivate();

// returns a boolean indicating the state of operation

Relationships

comments Relationship

$postWithComments = Post::with('comments')->get();

// returns a collection of all comments associated with the post

activeComments Relationship

$postWithActiveComments = Post::with('activeComments')->get();

// returns a collection of all active comments associated with the post

parent Relationship

$comment = Comments::latest()->first();

$comment->parent;

// returns the comment's parent if available

children Relationship

$comment = Comments::latest()->first();

$comment->children;

// returns the comment's children if available

ancestors Relationship

$comment = Comments::latest()->first();

$comment->ancestors;

// return the comment's ancestors if available

descendants Relationship

$comment = Comments::latest()->first();

$comment->descendants;

// return the comment's descendants if available

Additional functionalities

thanks to the great laravel-nestedset package, you have access to some additional functionalities, we review some of them here but you can always refer to the package's repository for the full documentation.

toTree()

$post->comments->toTree();

// returns a collection of the comment's tree structure associated with the post

toFlatTree()

$post->comments->toFlatTree();

// return a collection of the comment's flat tree structure associated with the post

saveAsRoot()

$comment = $post->comments()->latest()->first();

$comment->saveAsRoot();

// Implicitly change the comment's position to Root
// returns boolean

makeRoot()

$comment = $post->comments()->latest()->first();

$comment->makeRoot()->save();

// Explicitly change the comment's position to Root
// returns boolean

Credits

About

Implementing a Comment system for Laravel's Eloquent models.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages