1. Go to this page and download the library: Download melsaka/commentable 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/ */
use Melsaka\Commentable\CanComment;
class User extends Model
{
use CanComment;
// ...
}
use Melsaka\Commentable\HasComments;
class Post extends Model
{
use HasComments;
// ...
}
class Post extends Model
{
use HasComments;
public function commentsAreRated(): bool
{
return true; // return false by default
}
// ..
}
class Post extends Model
{
use HasComments;
public function commentsAreAccepted(): bool
{
return false; // return true by default
}
// ..
}
class User extends Model
{
use CanComment;
protected $fillable = [
'isAdmin',
// ..
];
public function commentsAreAccepted(): bool
{
return $this->isAdmin; // default false
}
// ..
}
use App\Models\User;
use App\Models\Post;
use Melsaka\Commentable\Models\Comment;
$post = Post::first();
$owner = User::first();
$parent = $post->comments()->first();
$data = [
'body' => 'this ia a new reply',
'accepted' => true,
'rate' => 4.5,
'parent_id' => $parent->id,
];
Comment::for($post)->via($owner)->add($data, $parent);
$data = 'this is a new parent comment';
(new Comment)->for($post)->via($owner)->add($data);
$post->addComment($data, $owner);
$owner->addComment($data, $post);
// To get the commentable model of a comment
$comment->commentable;
// To get the owner model of a comment
$comment->owner;
// To get the comment parent if it's a reply
$comment->parent;
// To get the comment replies if it's a parent comment
$comment->replies;
// Or maybe add some conditions
$comment->replies()->onlyAccepted()->get();
Comment::where($post->morphsArray())->get();
// which is similr to this
Comment::of($post);
// or
Comment::where($owner->morphsArray())->get();
// which is similr to this
Comment::by($owner);
// or
$post->commentsBy($owner);
$owner->commentsOn($owner);
Comment::where($owner->morphsArray())->get();
// Available filters:
Comment::of($post)->onlyParent()->get();
Comment::of($post)->onlyAccepted()->get();
Comment::of($post)->onlyRejected()->get();
Comment::of($post)->onlyRated()->get();
Comment::of($post)->onlyNotRated()->get();