1. Go to this page and download the library: Download ssntpl/laravel-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/ */
ssntpl / laravel-comments example snippets
namespace App\Models;
use Ssntpl\LaravelComments\Traits\HasComments;
class Post extends Model
{
use HasComments;
}
$model = Post::find(1);
$comment = $model->createComment([
// type: Optional. It represents the type of comment.
'type' => 'commentType',
// body: This is the body of the comment.
// (The `text` and `comment` keys are still accepted as backward-compatible aliases.)
'body' => 'This is the body of the comment',
// user_id: Optional. This is a foreign key belonging to that entity who is making the comment. For e.g:Users(so it will be the id of User who is making the comment).
'user_id' => 1,
// created_at: The created_at timestamp is automatically managed by Eloquent. Represents the time at which the comment is made. Otherwise one can manually assign a value to created_at when creating a new comment.
'created_at' => '2025-01-17 05:14:13',
]);
$card = Card::find(1);//comments can be added on a card
$card->comments; //return all the comments linked with the card
$card->comments() // returns the Illuminate\Database\Eloquent\Relations\MorphMany relation
$card->comments()->where('user_id',1)->get() //Accessing all comments of the card made for particular user
$comment = $card->comments()->where('id',3)->first() // One can access the specific comment of card
// to create an attachment on that comment
$comment->createFile([
'key' => 'path/filename.jpg',
'name' => 'filename.jpg'
]);
$comment->file //To access the first or only attachment with that comment on the card
$comment->files //To access all the attachments related to that comment on that card
//One can update particular comment by adding id as one of the params
$card->createComment(['id' => 23, 'user_id' => 2,'body'=> "This is a new comment"])
// The comment body is stored in the `body` column. For backward compatibility,
// `$comment->text` and `$comment->comment` both read from / write to `body`.
$comment->body; // "This is a new comment"
$comment->text; // same value (alias)
$comment->comment; // same value (alias)
//Like this one can delete all the comments along with its attachment on the card
$comments = $card->comments()->get();
foreach($comments as $comment) {
$comment->delete();
}
$reply = $card->createComment([
'user_id' => 2,
'body' => 'I agree',
'reply_to_comment_id' => $comment->id,
]);
$reply->replyToComment; // the parent comment
$comment->replies; // replies to this comment
$card->rootComments(); // top-level comments only (excludes replies)
$comment->react($user, 'thumbsup'); // add or change $user's reaction
$comment->unreact($user); // remove it
$comment->reactions; // all reactions on the comment
$comment->changelogs; // prior versions, newest first