TavaresEvora / commentable by TavaresEvora

A basic starting point for a flexible commentable system in Laravel
37
1
2
Package Data
Maintainer Username: TavaresEvora
Maintainer Contact: Tavares.evora.v@gmail.com (TavaresEvora)
Package Create Date: 2016-11-09
Package Last Update: 2017-11-07
Language: PHP
License: MIT
Last Refreshed: 2025-02-06 15:01:01
Package Statistics
Total Downloads: 37
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Commentable

Installation

Add commentable to your composer.json file to require :

    require : {
        ...
        "tavaresevora/commentable": "dev-master"
    }

Update Composer :

    composer update

The next required step is to add the service provider to config/app.php :

    Tavaresevora\Commentable\CommentableServiceProvider::class,

Migrate comments table

  php artisan vendor:publish --tag=comment-migrations
  php artisan migrate

Usage

Add commentable in model

    <?php
    
    namespace App;
   
    use Illuminate\Database\Eloquent\Model;
    use Tavaresevora\Commentable\Commentable;
    
    class Post extends Model
    {
        use Commentable;
        ...
    }

Add new comment

    <?php
    
    $post = App\Post::first();
    $user = Auth::user();
    $post->addComment('Superbe article !', $user);
    
    //addComment($body, Model $author, $validate = NULL);

update a comment

    <?php
    
    $post = App\Post::first();
    $comment = $post->comments->first();
    $user = Auth::user();
    $post->updateComment($comment, $body)
    
    // If you want change the author
    $newUser = User::find(2);
    $post->updateComment($comment, $body, $newUser)
    
    //updateComment(Comment $comment, $body, Model $author = NULL)

delete a comment

    <?php
    
    $post = App\Post::first();
    $comment = $post->comments->first();
    $post->deleteComment($comment)
    
    //deleteComment(Comment $comment)

get author of comment

    <?php
    
    @foreach($post->comments as $comment)
        <p>
            {{ $comment->body }} - <span>{{ $comment->author->name }}</span>
        </p>
    @endforeach