|
| 1 | +# Add comments to your Laravel application |
| 2 | + |
| 3 | +[](https://packagist.org/packages/beyondcode/laravel-comments) |
| 4 | +[](https://travis-ci.org/beyondcode/laravel-comments) |
| 5 | +[](https://scrutinizer-ci.com/g/beyondcode/laravel-comments) |
| 6 | +[](https://packagist.org/packages/beyondcode/laravel-comments) |
| 7 | + |
| 8 | +Add the ability to associate comments to your Laravel Eloquent models. The comments can be approved and nested. |
| 9 | + |
| 10 | +```php |
| 11 | +$post = Post::find(1); |
| 12 | + |
| 13 | +$post->comment('This is a comment'); |
| 14 | + |
| 15 | +$post->commentAsUser($user, 'This is a comment from someone else'); |
| 16 | +``` |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +You can install the package via composer: |
| 21 | + |
| 22 | +```bash |
| 23 | +composer require beyondcode/laravel-comments |
| 24 | +``` |
| 25 | + |
| 26 | +The package will automatically register itself. |
| 27 | + |
| 28 | +You can publish the migration with: |
| 29 | + |
| 30 | +```bash |
| 31 | +php artisan vendor:publish --provider=BeyondCode\Comments\CommentsServiceProvider --tag="migrations" |
| 32 | +``` |
| 33 | + |
| 34 | +After the migration has been published you can create the media-table by running the migrations: |
| 35 | + |
| 36 | +```bash |
| 37 | +php artisan migrate |
| 38 | +``` |
| 39 | + |
| 40 | +You can publish the config-file with: |
| 41 | + |
| 42 | +```bash |
| 43 | +php artisan vendor:publish --provider=BeyondCode\Comments\CommentsServiceProvider --tag="config" |
| 44 | +``` |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +### Registering Models |
| 49 | + |
| 50 | +To let your models be able to receive comments, add the `HasComments` trait to the model classes. |
| 51 | + |
| 52 | +``` php |
| 53 | +namespace App\Models; |
| 54 | + |
| 55 | +use Beyondcode\Comments\HasComments; |
| 56 | +use Illuminate\Database\Eloquent\Model; |
| 57 | + |
| 58 | +class Post extends Model |
| 59 | +{ |
| 60 | + use HasComments; |
| 61 | + ... |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Creating Comments |
| 66 | + |
| 67 | +To create a comment on your commentable models, you can use the `comment` method. It receives the string of the comment that you want to store. |
| 68 | + |
| 69 | +```php |
| 70 | +$post = Post::find(1); |
| 71 | + |
| 72 | +$comment = $post->comment('This is a comment from a user.'); |
| 73 | +``` |
| 74 | + |
| 75 | +The comment method returns the newly created comment class. |
| 76 | + |
| 77 | +Sometimes you also might want to create comments on behalf of other users. You can do this using the `commentAsUser` method and pass in your user model that should get associated |
| 78 | +with this comment: |
| 79 | + |
| 80 | +```php |
| 81 | +$post = Post::find(1); |
| 82 | + |
| 83 | +$comment = $post->commentAsUser($yourUser, 'This is a comment from someone else.'); |
| 84 | +``` |
| 85 | + |
| 86 | +### Approving Comments |
| 87 | + |
| 88 | +By default, all comments that you create are not approved - this is just a boolean flag called `is_approved` that you can use in your views/controllers to filter out comments that you might not yet want to display. |
| 89 | + |
| 90 | +To approve a single comment, you may use the `approve` method on the Comment model like this: |
| 91 | + |
| 92 | +```php |
| 93 | +$post = Post::find(1); |
| 94 | +$comment = $post->comments->first(); |
| 95 | + |
| 96 | +$comment->approve(); |
| 97 | +``` |
| 98 | + |
| 99 | +### Auto Approve Comments |
| 100 | + |
| 101 | +If you want to automatically approve a comment for a specific user (and optionally model) you can let your User model implement the following interface and method: |
| 102 | + |
| 103 | +```php |
| 104 | +namespace App\Models; |
| 105 | + |
| 106 | +use BeyondCode\Comments\Contracts\Commentator; |
| 107 | +use Illuminate\Foundation\Auth\User as Authenticatable; |
| 108 | + |
| 109 | +class User extends Authenticatable implements Commentator |
| 110 | +{ |
| 111 | + /** |
| 112 | + * Check if a comment for a specific model needs to be approved. |
| 113 | + * @param mixed $model |
| 114 | + * @return bool |
| 115 | + */ |
| 116 | + public function needsCommentApproval($model): bool |
| 117 | + { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +The `needsCommentApproval` method received the model instance that you want to add a comment to and you can either return `true` to mark the comment as **not** approved, or return `false` to mark the comment as **approved**. |
| 125 | + |
| 126 | +### Retrieving Comments |
| 127 | + |
| 128 | +The models that use the `HasComments` trait have access to it's comments using the `comments` relation: |
| 129 | + |
| 130 | +```php |
| 131 | + |
| 132 | +$post = Post::find(1); |
| 133 | + |
| 134 | +// Retrieve all comments |
| 135 | +$comments = $post->comments; |
| 136 | + |
| 137 | +// Retrieve only approved comments |
| 138 | +$approved = $post->comments()->approved()->get(); |
| 139 | + |
| 140 | +``` |
| 141 | + |
| 142 | +### Testing |
| 143 | + |
| 144 | +``` bash |
| 145 | +composer test |
| 146 | +``` |
| 147 | + |
| 148 | +### Changelog |
| 149 | + |
| 150 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 151 | + |
| 152 | +## Contributing |
| 153 | + |
| 154 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 155 | + |
| 156 | +### Security |
| 157 | + |
| 158 | +If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker. |
| 159 | + |
| 160 | +## Credits |
| 161 | + |
| 162 | +- [Marcel Pociot](https://github.com/mpociot) |
| 163 | +- [All Contributors](../../contributors) |
| 164 | + |
| 165 | +## License |
| 166 | + |
| 167 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments