Skip to content

Implementing a Comment system for Laravel's Eloquent models.

License

Notifications You must be signed in to change notification settings

babakhakimi/Laravel-Commentable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 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 babakhakimi/laravel-commentable

Publish and Run the migrations

php artisan vendor:publish --provider="BabakHakimi\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' => [
	...
	\BabakHakimi\LaravelCommentable\CommentableServiceProvider::class,
],

Setup models - just use the Trait in the Model.

<?php

namespace App;

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

class Post extends Model
{
	use Commentable;

}

Usage

use App\User;
use App\Post;
use BabakHakimi\LaravelCommentable\Comment;



//  assuming that we have these variables
$user = User::first();
$post = Post::first();
$comment = 
[
	'title' => 'comment title (nullable)', 
	'body' => 'comment body'
];
$commentId = 1;

Create a comment

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

Create a child comment

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

Check if a comment has children (boolean)

    $comment = Comment::first();
    $comment->hasChildren(); 

Update a comment

    $post->updateComment($commentId, $comment);

Delete a comment

    $post->deleteComment($commentId); 

Count comments

    $post->commentCount();

Activation

by default when you add a cooment it is stored as a deactive comment, unless you provide an 'active' field and set it to true:

$activeComment = 
[
	'title'  => 'comment title (nullable)', 
	'body'   => 'comment body',
	'active' => true
];
$post->comment($activeComment, $user);

but you can always change the comment state by using below methods:

Activate

    $post->active();
    // returns true if operation is successful

Deactivate

    $post->deactive();
    // returns true if operation is successful

Relationships

comments Relationship

    $postWithComments = Post::with('comments')
	    ->get();
    // return all comments associated with the post

activeComments Relationship

    $postWithActiveComments = Post::with('activeComments')
	    ->get();
    // return all active comments associated with the post

parent Relationship

    $comment = Post::first()->comments()->first();
    
    $comment->parent;
    // return the comment's parent if available

children Relationship

    $comment = Post::first()->comments()->first();
    
    $comment->children;
    // return the comment's children if any

ancestors Relationship

    $comment = Post::first()->comments()->first();
    
    $comment->ancestors;
    // return the comment's ancestors if any

descendants Relationship

    $comment = Post::first()->comments()->first();
    
    $comment->descendants;
    // return the comment's descendants if any

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::first()->comments->toTree();
    
    // return a collection of the comment's tree structure

toFlatTree()

    Post::first()->comments->toFlatTree();
    
    // return a collection of the comment's flat tree structure

saveAsRoot()

    $comment = Post::first()->comments()->latest()->first();
    $comment->saveAsRoot();
    
    // Implicitly change the comment's position to Root
    // return bool

makeRoot()

    $comment = Post::first()->comments()->latest()->first();
    $comment->makeRoot()->save();
    
    // Explicitly change the comment's position to Root
    // return bool

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

  • PHP 100.0%