faustbrian/laravel-commentable

Commentable Polymorphic Eloquent Models for for Laravel 5

v3.1.3 2018-09-16 04:50 UTC

README

Build Status PHP from Packagist Latest Version License

Installation

Require this package, with Composer, in the root directory of your project.

$ composer require faustbrian/laravel-commentable

To get started, you'll need to publish the vendor assets and migrate:

php artisan vendor:publish --provider="BrianFaust\Commentable\CommentableServiceProvider" && php artisan migrate

Usage

Setup a Model

<?php

namespace App;


use BrianFaust\Commentable\Traits\HasComments;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasComments;
}

Create a comment

$user = User::first();
$post = Post::first();

$comment = $post->comment([
    'title' => 'Some title',
    'body' => 'Some body',
], $user);

dd($comment);

Create a comment as a child of another comment (e.g. an answer)

$user = User::first();
$post = Post::first();

$parent = $post->comments->first();

$comment = $post->comment([
    'title' => 'Some title',
    'body' => 'Some body',
], $user, $parent);

dd($comment);

Update a comment

$comment = $post->updateComment(1, [
    'title' => 'new title',
    'body' => 'new body',
]);

Delete a comment

$post->deleteComment(1);

Count comments an entity has

$post = Post::first();

dd($post->commentCount());

Testing

$ phpunit

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@brianfaust.me. All security vulnerabilities will be promptly addressed.

Credits

License

MIT © Brian Faust