Download the PHP package alayubi/laravel-comment without Composer
On this page you can find all versions of the php package alayubi/laravel-comment. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-comment
Instalation
you must publish the migration with:
and rerun the migration.
Config
You can publish the config file with:
Usage
Commentable
If you want a model can be commented you can implements \Lara\Comment\Contracts\IsCommentable
interface and add \Lara\Comment\Commentable
trait for the implementation.
Commentator
Commentator is a model that comment on a model.
Implements \Lara\Comment\Contracts\IsCommentator\
interface on a model if you want to your model to be a commentator and add \Lara\Comment\Commentator
trait for the implementation.
Creating Comments
To create a comment to a model you should first create the view with form.
at least you provide the textarea tag HTML with comment
name.
Then you may create a route to handle the request.
In your controller you can use \Lara\Comment\CommentService
class and store
method to create a comment.
Updating comment
Destory comment
Frontend
Before you start with the default frontend you must integrate your app with:
- vue
- tailwindcss
The command below will publish the nested comment frontend. With nested comment you can reply a comment. You can publish the frotend with:
it will create views in resources/views/vendor/comment and vue component in resources/js/components/comment directory. Don't forget to copy this code below to your app.js.
After published you are be able to customise the css to fit with your view. To use the frontend nested comment you may include it in your view and pass the commentable model to it.
The code will render the comments with nested indentation belongs to $post
.
Change the indentation
in config file so that you can reply a comment in more deeper indentation.
You can imagine the indentation like:
Routes
By default there are three routes for common task.
- create
visit
route('comments.comments.store')
or/comments/{comment}/comments
with POST method to create a comment on the comment. - update
visit
route('comments.update')
or/comments/{comment}
with PUT method to update the comment. - destroy
visit
route('comments.destroy')
or/comments/{comment}
with DELETE method to remove the comment from storage.
If you don't want to use the default route put false
value in route
inside setting file comment.php.
Validation Rule and Requested Data
\Lara\Comment\Validation\DefaultValidator
is the default validator.
Validator is a class that responsible to get and validate the data from user.
If you want to chnage default behavior of validation or what kind of data you will store to storage you can extends \Lara\Comment\Validation\Validator
abstract class then you must implement public function data() and public function rules().
From the class you can access commentator model and request object.
data()
The data function is resposible to return what kind of data to store.
rules()
The rules function is responsible to define what kind of validation rules to run.
Don't forget to change the config validator value to your implementation.
validateWithBag() method
If you have multiple form for comment then you want to display error message
You can use validateWithBag()
method to validate with bag.
so whene validation error occur you may the access the error bag
you can access the name error bag with combination of the commentable
id and the method PUT
and POST
Redirector
Redirector will redirect to the URL if validation fails. The default redirector is \Lara\Comment\Redirect\RedirectBack. This will redirect back with URL fragment #validation-comment-error. If you wish to change this default behavior you could create your own redirect by extends \Lara\Comment\Redirect\Redirect abstract class and change the redirector value on configuration comment file to your own implementation.
Policy
You can create your own policy to authorize the action. To create policy class just run the laravel artisan command. For the complete guide see laravel documentation.
Don't forget to change policy
class in config file.