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',
// text: This is text of the comment.
'text' => 'The is the text 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,'text'=> "This is a new comment"])
//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();
}