This is a comment management package for any object in Laravel that you can use in your projects.
Run the following command to pull in the latest version:
composer require jobmetric/laravel-comment
This package evolves every day under continuous development and integrates a diverse set of features. It's a must-have asset for Laravel enthusiasts and provides a seamless way to align your projects with basic comment models.
In this package, you can use it seamlessly with any model that needs comments.
Now let's go to the main function.
php artisan migrate
Meet the HasComment
class, meticulously designed for integration into your model. This class automates essential tasks, ensuring a streamlined process for:
In the first step, you need to connect this class to your main model.
use JobMetric\Comment\HasComment;
class Post extends Model
{
use HasComment;
}
When you add this class, you will have to implement CommentContract
to your model.
use JobMetric\Comment\Contracts\CommentContract;
class Post extends Model implements CommentContract
{
use HasComment;
}
Now you have to use the needsCommentApproval function, and you have to add it to your model.
use JobMetric\Comment\Contracts\CommentContract;
class Post extends Model implements CommentContract
{
use HasComment;
public function needsCommentApproval(): bool
{
return false;
}
}
You can now use the HasComment
class for your model.
You can store a new comment by using the following code:
$post = Post::create([
'title' => 'Post title',
'body' => 'Post body',
]);
$post->comment('This is a comment', $parent_comment_id, $user_id);
If you want to store a comment without a parent comment, you can pass
null
to the second parameter.If you want to store a comment without a user, you can pass
null
to the third parameter.
You can update an existing comment by using the following code:
$post->updateComment($comment->id, 'This is a new comment');
You can forget an existing comment by using the following code:
$post->forgetComment($comment->id);
You can forget all comments by using the following code:
$post->forgetComments();
You can get all comments by using the following code:
$comments = $post->comments;
You can get all approved comments by using the following code:
$comments = $post->approvedComments;
You can get all unapproved comments by using the following code:
$comments = $post->unapprovedComments;
You can get all parent comments by using the following code:
$comments = $post->parentComments;
You can also connect the CanComment
trait class to your User model.
use JobMetric\Comment\CanComment;
class User extends Authenticatable
{
use CanComment;
}
This package contains several events for which you can write a listener as follows
Event | Description |
---|---|
CommentStoredEvent |
This event is called after storing the comment. |
CommentUpdateEvent |
This event is called after updating the comment. |
CommentForgetEvent |
This event is called after forgetting the comment. |
Thank you for considering contributing to the Laravel Comment! The contribution guide can be found in the CONTRIBUTING.md.
The MIT License (MIT). Please see License File for more information.