Package for using comments with Vue.js component
composer require yarmat/laravel-comment
You must publish Vue Component, Languages, migration and config;
php artisan vendor:publish --provider="Yarmat\Comment\CommentServiceProvider"
After publishing you can create comments-tables by running the migrations:
php artisan migrate
Add middleware
protected $routeMiddleware = [
...
'comment' => 'Yarmat\Comment\Http\Middleware\CommentMiddleware'
];
You should edit config file
config/comment.php
Limit of comments when will you send a request for the route get (route('comment.get'))
'limit' => '5',
'default_order' => 'DESC', // OR ASC. DESC - Newest, ASC - Oldest
Change the User Model
'models' => [
'user' => 'Your User Model Path'
]
Change prefix, if you need to change route prefix
{prefix}.store {prefix}.destroy {prefix}.update {prefix}.count {prefix}.get
You can assign a middleware to each route:
'middleware' => [
'store' => ['throttle:15'],
'destroy' => ['auth'],
'get' => [],
'update' => [],
'count' => []
],
'models_with_comments' => [
'Blog' => App\Models\Blog::class,
],
Add relations for you comments
'comment_relations' => ['user'],
or
'comment_relations' => ['user' => function($query) {
$query->select(['id', 'name', 'email']);
}, 'likes' => function($query){
$query-> ...
}],
'auth' => [ // For Auth Users
'message' => ['required', 'string', new \Yarmat\Comment\Rules\Spam(), new \Yarmat\Comment\Rules\AllowableSite()]
],
'not_auth' => [ // For Not Auth Users
'name' => 'required|alpha',
'email' => 'required|email',
'message' => ['required', 'string', new \Yarmat\Comment\Rules\Spam(), new \Yarmat\Comment\Rules\AllowableSite()]
],
'messages' => []
Function that transform Comments Model before you get it in the Vue component
'transformFunction' => function ($item) {
return [
'id' => $item->id,
'message' => $item->message,
'isVisibleForm' => false,
'date' => \Date::parse($item->created_at)->diffForHumans(),
'user' => [
'name' => $item->user->name ?? $item->name,
'email' => $item->user->email ?? $item->email
],
'children' => []
];
},
Php function strip_tags() that cuts out all tags except those that you list in the string
'allowable_tags' => '',
You can list spam words. Comments with these words will not be published.
'spam_list' => ['spam'],
You can list allowable sites. The comment will not be published if there is an unresolved link in it.
'allowable_sites' => ['site.com'],
F.e. you have model Post and you want to attach comments to it
Add model to config
'models_with_comments' => [
'Post' => App\Post::class,
],
Add Yarmat\Comment\Traits\HasCommentTrait and Yarmat\Comment\Contracts\CommentContract to your model Post:
<?php
use Illuminate\Database\Eloquent\Model;
use Yarmat\Comment\Contracts\CommentContract;
use Yarmat\Comment\Traits\HasCommentTrait;
class Post extends Model implements CommentContract
{
use HasCommentTrait;
}
Add Yarmat\Comment\Traits\CommenterTrait to your User model
<?php
use Illuminate\Foundation\Auth\User as Authenticatable;
use Yarmat\Comment\Traits\CommenterTrait;
class User extends Authenticatable
{
use CommenterTrait;
}
Include comment component to your Vue App
Vue.component('comment-component', require('./components/comment/CommentComponent').default);
Include to your View
<comment-component></comment-component>
Include config to your View
{!! \Comment::config('Post', $post->id) !!}
That is all! The you can customise vue component to your template. Component is here:
resources/js/components/comment
The Laravel-comment is open-sourced software licensed under the MIT license.