Download the PHP package alfonsobries/laravel-commentable without Composer
On this page you can find all versions of the php package alfonsobries/laravel-commentable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alfonsobries/laravel-commentable
More information about alfonsobries/laravel-commentable
Files in alfonsobries/laravel-commentable
Package laravel-commentable
Short Description A Laravel package to add comments to any model
License MIT
Informations about the package laravel-commentable
Laravel Commentable
A Laravel package to add comments to any model
Use
Installation
-
Install the composer package:
-
Publish the database migration
-
Optionally publish the config file
-
Configure the model that is going to receive the comments with the
CommentableInterfacecontract and add theCommentabletrait. - Add the
CanCommentInterfacecontract and theCanCommenttrait to the model that is going to add the comment (usually theUserModel). Since you can add anonymous comments this is only necessary if you want to accept User comments.
Add comments
-
Use the
addCommentmethod to add an anonymous comment -
Use the
addCommentFrommethod to add a comment from a User (or the model that implements theCanCommentInterfacecontract) - You can also comment with the User model (or the model that implements the
CanCommentInterfacecontract) by using thecommentmethod.
Add replies to comments
-
The
Commentmethod is another commentable instance, meaning you can use theaddCommentoraddCommentFrommethod to add a reply. -
You can also use the
replyandreplyFrommethods that are just an alias for the comment methods above. -
Use the
addCommentFrommethod to add a comment from the User (or the model that implements theCanCommentInterfacecontract) - You can also comment with the User model (or the model that implements the
CanCommentInterfacecontract) by using thecommentmethod.
Get comments
-
You can get all the user comments with the
commentsmethod - You can get all the comments associated with the commentable model with the
commentsmethod
Sort comments
Sort the comments by popularity (average positive likes) by using the popular and unpopular scope methods.
Update comments
Since the Comment object is just a regular Eloquent Model you can use any of the different ways to update the models.
Approving comments
By default, all the new comments are unapproved (approved_at=null), meaning that you need to approve them manually based on your specific needs (you can add an event listener based on the the events listed below to do that). If you don't need to handle approved and unapproved comments, you can simply ignore the filter when querying the comments.
You can filter the approved or not approved comments with the approved and notApproved methods.
Events
The Comment model fires the following events that you can listen:
- CommentCreated
- CommentCreating
- CommentUpdated
- CommentUpdating
- CommentDeleted
- CommentDeleting
- CommentSaved
- CommentSaving
Development
Analyze the code with phpstan
Refactor the code with php rector
Format the code with php-cs-fixer
Run tests
Security
If you discover a security vulnerability within this package, please write trough the https://alfonsobries.com contact form. All security vulnerabilities will be promptly addressed.
Credits
This project exists thanks to all the people who contribute.
License
Alfonso Bribiesca