Download the PHP package melsaka/commentable without Composer
On this page you can find all versions of the php package melsaka/commentable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download melsaka/commentable
More information about melsaka/commentable
Files in melsaka/commentable
Package commentable
Short Description Implementing a Comment system for Laravel's Eloquent models.
License MIT
Informations about the package commentable
Commentable
The Commentable package for Laravel provides a convenient way to manage comments and comment-related functionality in your application. This documentation will guide you through the usage of this package.
Table of Contents
- Installation
- Configuration
- Usage
- Adding Comments
- Editing Comments
- Removing Comments
- Changing Comment Status
- Rating Comments
- Getters Methods
- Relation Methods
- Filters
- Checkers
- Commentable Table
- License
Installation
Add the package to your Laravel app via Composer:
Register the package's service provider in config/app.php.
Run the migrations to add the required table to your database:
Add CanComment
trait to the comment owner model, User
model for example:
Add HasComments
trait to your commentable model, Post
model for example:
If you want to enable rating
feature in any commentable model (Post
model in this case) add commentsAreRated()
method.
By default, comments are not rated false. You can change this by adding this method and making it return true.
By default, new comments are accepted, but sometimes you don't want to approve comments for all users;
In this case add commentsAreAccepted()
method and make it return false, it return true by default.
The owner model that uses the CanComment trait also has a commentsAreAccepted()
method, which returns false by default.
You can make this method return true for admin users only for emaple, so all admin users comments be accepted automatically.
Configuration
To configure the package, publish its configuration file:
You can then modify the configuration file to change the comments table name if you want, default: comments
.
Usage
Adding Comments
To add a new comment, you can use the add
method after for
and via
methods like this:
-
$post
is the commentable model. -
$owner
is the owner of the comment. -
$data
can be a string or an array holding comment data. $parent
is an optional parameter to add the comment as a reply to a parent comment, default:null
.
Note: We may use $post
, $owner
, $parent
variables in the upcoming examples.
You can also add comments in a many different ways:
Note: all these methods accept $parent
param but it's null
by default. so you can do this as well:
Editing Comments
To edit a comment, use the edit
method and pass the $comment
you want to edit as the first parameter:
-
$comment
can be the comment's ID, or the comment itself. -
$data
can be a string or an array containing the updated comment data. $parent
is optional and has a default value of null.
You can also edit comments using different methods:
In this example: $comment->for($post)->editTo($data);
, The for method reduces the queries executed.
But you can edit comments without it since we fetch the commentable model anyway, if you don't provide it in a for method.
Also the editComment
method won't update, if comment commentable_id
or owner_id
is not the same as $post->id
or $owner->id
.
Note: all these methods accept $parent
param but it's null
by default. so you can do this as well:
You can also add a parent to a comment using addParent()
method
Removing Comments
To remove one or more comments, use the remove
method:
$comment
can be a Comment instance, a collection of comments, an array of comment IDs, or a single comment ID.
To remove a single comment instance, you can also use:
You can remove comments in different ways:
The deleteComment
and removeComment
methods won't remove $comment
, if comment commentable_id
or owner_id
is not the same as $post->id
or $owner->id
.
Changing Comment Status
You can change the comment status to accepted using the accept
method:
To reject a comment, use the reject
method:
You can also accept or reject comments on a post:
The acceptComment
and rejectComment
methods won't update $comment
status, if comment commentable_id
or owner_id
is not the same as $post->id
or $owner->id
.
Rating Comments
To change the rating of a comment, use the rateIt
method:
You can also rate a comment using these methods:
rateComment
returns false if $post
commentsAreRated
is false or commentable_id
is not the same as $post->id
.
You can also get the post average rate using averageRate()
method.
Getters Methods
To get comment by id, you can use Comment::getCommentOfId($id)
it returns 404 if id not exist.
Note: if you give it a valid $comment
instance it will return it back to you.
You can also use various getter methods to retrieve comments:
Get Post Comments
Get User Comments
Get Post Comments by User
Eager Load Comment Replies and Replies Count
Get Only Accepted Replies
Get Only Rejected Replies
These methods can also accept callback functions for further customization.
You can use callback function within the with
, withCount
, load
and loadCount
methods:
You can also use with
and load
methods from commentable and owner models like this:
These methods accept callback
function as well.
You can also load replies
/repliesCount
.
And all of them accept a callback function:
Relation Methods
Comment
Model has this relation setup that you may need to use in your app:
You can get the comments
from related
model like this:
Every related model CanComment
or HasComments
have these built-in methods as well:
You can use the morphsArray()
method to filter by commentable
or owner
like this:
But the difference is that (of
, by
) methods return onlyParent
comments withRepliesCount
.
While this code:
Returns comments
and replies
toghter without reply count and you will have to check the parent_id
to know which is the parent
comment and which is the reply
.
Filters
If you want to filter the comments you can use these methods:
You can also chain filters like this:
Checkers
You can Check if $comment
has replies
or parent
You can also check if a commentable
model hasCommentsBy
an owner
or the other way around:
Check if a $comment
belongs to a related Model
:
Commentable Table
The structure of the coments table is as follows:
License
This package is released under the MIT license (MIT).