1. Go to this page and download the library: Download tizis/lara-comments library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tizis / lara-comments example snippets
use tizis\laraComments\Traits\Commenter;
class User extends Authenticatable {
use ..., Commenter;
use tizis\laraComments\Entity\Comment as laraComment;
class Comment extends laraComment
{
}
use tizis\laraComments\Contracts\ICommentable;
use tizis\laraComments\Traits\Commentable;
class Post extends Model implements ICommentable {
use Commentable;
namespace App\Http\Policies;
use App\Entity\Comment;
use tizis\laraComments\Policies\CommentPolicy as CommentPolicyPackage;
class CommentPolicy extends CommentPolicyPackage
{
// overwrite delete rule
public function delete($user, $comment): bool
{
// ever true
return true;
}
}
use Illuminate\Support\Facades\Gate;
use App\Http\Policies\CommentPolicy;
...
public function boot()
{
Gate::resource('comments_custom', CommentPolicy::class, [
'delete' => 'delete',
'reply' => 'reply',
'edit' => 'edit',
'vote' => 'vote',
'store => 'store'
]);
}
$user = Auth::user();
$modelId = decrypt($request->commentable_encrypted_key)['id']; // get model id from encrypted model key
$model = $model = Post::findOrFail($modelId);
$message = '123'
$parent = rand(1, 100); // optional
$createdComment = CommentService::createComment(new Comment(), $user, $model, $message, [optional $parent]);
namespace App\Helpers\CommentPreprocessor;
use tizis\laraComments\Contracts\ICommentPreprocessor;
class Comment implements ICommentPreprocessor
{
public function process($comment): array
{
return 'Hi, ' . $comment . '!';
}
}
namespace App\Helpers\CommentPreprocessor;
use tizis\laraComments\Contracts\ICommentPreprocessor;
class User implements ICommentPreprocessor
{
public function process($user): array
{
$user->name = $user->name . '[Moderator]'
return $user;
}
}