├── config └── comments.php ├── database ├── factories │ └── CommentFactory.php └── migrations │ └── create_comments_table.php ├── src ├── Contracts │ └── IsComment.php ├── CommentsServiceProvider.php ├── Concerns │ └── HasComments.php └── Models │ └── Comment.php ├── LICENSE.md ├── .php-cs-fixer.dist.php ├── CHANGELOG.md ├── composer.json └── README.md /config/comments.php: -------------------------------------------------------------------------------- 1 | \RyanChandler\Comments\Models\Comment::class, 6 | 7 | /** @phpstan-ignore-next-line */ 8 | 'user' => \App\Models\User::class, 9 | 10 | ]; 11 | -------------------------------------------------------------------------------- /database/factories/CommentFactory.php: -------------------------------------------------------------------------------- 1 | $this->faker->words(rand(3, 10), asText: true), 16 | ]; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Contracts/IsComment.php: -------------------------------------------------------------------------------- 1 | name('laravel-comments') 14 | ->hasConfigFile(); 15 | } 16 | 17 | public function packageBooted() 18 | { 19 | $this->loadMigrationsFrom([ 20 | __DIR__ . '/../database/migrations', 21 | ]); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /database/migrations/create_comments_table.php: -------------------------------------------------------------------------------- 1 | id(); 13 | $table->unsignedBigInteger('parent_id')->nullable()->index(); 14 | $table->unsignedBigInteger('user_id')->index()->nullable(); 15 | $table->morphs('commentable'); 16 | $table->longText('content'); 17 | $table->timestamps(); 18 | $table->softDeletes(); 19 | }); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /src/Concerns/HasComments.php: -------------------------------------------------------------------------------- 1 | */ 16 | public function comments(): MorphMany 17 | { 18 | return $this->morphMany(config('comments.model'), 'commentable'); 19 | } 20 | 21 | public function comment(string $content, Model $user = null, IsComment $parent = null): IsComment 22 | { 23 | return $this->comments()->create([ 24 | 'content' => $content, 25 | 'user_id' => $user ? $user->getKey() : Auth::id(), 26 | 'parent_id' => $parent?->getKey(), 27 | ]); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Models/Comment.php: -------------------------------------------------------------------------------- 1 | morphTo(); 23 | } 24 | 25 | public function user(): BelongsTo 26 | { 27 | return $this->belongsTo(config('comments.user'), 'user_id'); 28 | } 29 | 30 | public function parent(): BelongsTo 31 | { 32 | return $this->belongsTo(static::class, 'parent_id'); 33 | } 34 | 35 | public function children(): HasMany 36 | { 37 | return $this->hasMany(static::class, 'parent_id'); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ryangjchandler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-comments` will be documented in this file. 4 | 5 | ## v1.0.0 - 2024-03-12 6 | 7 | * Support for Laravel 11.x 8 | * Drop support for Laravel 9.x and 10.x, as well as PHP 8.1. 9 | 10 | ## v0.2.0 - 2023-03-24 11 | 12 | ### What's Changed 13 | 14 | - build(deps): bump dependabot/fetch-metadata from 1.3.1 to 1.3.3 by @dependabot in https://github.com/ryangjchandler/laravel-comments/pull/1 15 | - Update tag in publishing config file by @faisuc in https://github.com/ryangjchandler/laravel-comments/pull/2 16 | - composer: allow plugins for the 'run-tests' action by @medilies in https://github.com/ryangjchandler/laravel-comments/pull/3 17 | - build(deps): bump dependabot/fetch-metadata from 1.3.3 to 1.3.4 by @dependabot in https://github.com/ryangjchandler/laravel-comments/pull/5 18 | - build(deps): bump dependabot/fetch-metadata from 1.3.4 to 1.3.5 by @dependabot in https://github.com/ryangjchandler/laravel-comments/pull/6 19 | - build(deps): bump dependabot/fetch-metadata from 1.3.5 to 1.3.6 by @dependabot in https://github.com/ryangjchandler/laravel-comments/pull/8 20 | - chore: add support for Laravel 10 by @ryangjchandler in https://github.com/ryangjchandler/laravel-comments/pull/9 21 | 22 | ### New Contributors 23 | 24 | - @dependabot made their first contribution in https://github.com/ryangjchandler/laravel-comments/pull/1 25 | - @faisuc made their first contribution in https://github.com/ryangjchandler/laravel-comments/pull/2 26 | - @medilies made their first contribution in https://github.com/ryangjchandler/laravel-comments/pull/3 27 | - @ryangjchandler made their first contribution in https://github.com/ryangjchandler/laravel-comments/pull/9 28 | 29 | **Full Changelog**: https://github.com/ryangjchandler/laravel-comments/compare/v0.1.0...v0.2.0 30 | 31 | ## v0.1.0 - 2022-06-23 32 | 33 | **Full Changelog**: https://github.com/ryangjchandler/laravel-comments/commits/v0.1.0 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ryangjchandler/laravel-comments", 3 | "description": "A dead-simple comments package for Laravel.", 4 | "keywords": [ 5 | "ryangjchandler", 6 | "laravel", 7 | "laravel-comments" 8 | ], 9 | "homepage": "https://github.com/ryangjchandler/laravel-comments", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Ryan Chandler", 14 | "email": "support@ryangjchandler.co.uk", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.2", 20 | "illuminate/contracts": "^11.0", 21 | "illuminate/database": "^11.0", 22 | "spatie/laravel-package-tools": "^1.16.3" 23 | }, 24 | "require-dev": { 25 | "friendsofphp/php-cs-fixer": "^3.8", 26 | "nunomaduro/collision": "^8.0", 27 | "orchestra/testbench": "^9.0", 28 | "pestphp/pest": "^2.0", 29 | "pestphp/pest-plugin-laravel": "^2.0", 30 | "phpstan/extension-installer": "^1.1", 31 | "phpstan/phpstan-deprecation-rules": "^1.0", 32 | "phpstan/phpstan-phpunit": "^1.0", 33 | "phpunit/phpunit": "^10.0", 34 | "spatie/laravel-ray": "^1.35" 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "RyanChandler\\Comments\\": "src", 39 | "RyanChandler\\Comments\\Database\\Factories\\": "database/factories" 40 | } 41 | }, 42 | "autoload-dev": { 43 | "psr-4": { 44 | "RyanChandler\\Comments\\Tests\\": "tests" 45 | } 46 | }, 47 | "scripts": { 48 | "analyse": "vendor/bin/phpstan analyse", 49 | "test": "vendor/bin/pest", 50 | "test-coverage": "vendor/bin/pest --coverage", 51 | "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" 52 | }, 53 | "config": { 54 | "sort-packages": true, 55 | "allow-plugins": { 56 | "pestphp/pest-plugin": true, 57 | "phpstan/extension-installer": true 58 | } 59 | }, 60 | "extra": { 61 | "laravel": { 62 | "providers": [ 63 | "RyanChandler\\Comments\\CommentsServiceProvider" 64 | ], 65 | "aliases": { 66 | "Comments": "RyanChandler\\Comments\\Facades\\Comments" 67 | } 68 | } 69 | }, 70 | "minimum-stability": "dev", 71 | "prefer-stable": true 72 | } 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A dead-simple comments package for Laravel. 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/laravel-comments.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/laravel-comments) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/laravel-comments/run-tests?label=tests)](https://github.com/ryangjchandler/laravel-comments/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/laravel-comments/Check%20&%20fix%20styling?label=code%20style)](https://github.com/ryangjchandler/laravel-comments/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/laravel-comments.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/laravel-comments) 7 | 8 | This package provides an incredibly simple comment system for your Laravel applications. 9 | 10 | > If you're looking for a package with UI components, I highly recommend using [Spatie's `laravel-comments`](https://laravel-comments.com/) package which comes with Livewire support out of the box. 11 | 12 | ## Installation 13 | 14 | You can install the package via Composer: 15 | 16 | ```bash 17 | composer require ryangjchandler/laravel-comments 18 | ``` 19 | 20 | The package automatically registers migrations so there's no need to publish anything, just run them. 21 | 22 | ``` 23 | php artisan migrate 24 | ``` 25 | 26 | You can publish the config file with: 27 | 28 | ```bash 29 | php artisan vendor:publish --tag="comments-config" 30 | ``` 31 | 32 | This is the contents of the published config file: 33 | 34 | ```php 35 | return [ 36 | 37 | 'model' => \RyanChandler\Comments\Models\Comment::class, 38 | 39 | 'user' => \App\Models\User::class, 40 | 41 | ]; 42 | ``` 43 | 44 | ## Usage 45 | 46 | Start by using the `RyanChandler\Comments\Concerns\HasComments` trait on your model. 47 | 48 | ```php 49 | use RyanChandler\Comments\Concerns\HasComments; 50 | 51 | class Post extends Model 52 | { 53 | use HasComments; 54 | } 55 | ``` 56 | 57 | This trait adds a `comments(): MorphMany` relationship on your model. It also adds a new `comment()` method that can be used to quickly add a comment to your model. 58 | 59 | ```php 60 | $post = Post::first(); 61 | 62 | $post->comment('Hello, world!'); 63 | ``` 64 | 65 | By default, the package will use the authenticated user's ID as the "commentor". You can customise this by providing a custom `User` to the `comment()` method. 66 | 67 | ```php 68 | $post->comment('Hello, world!', user: User::first()); 69 | ``` 70 | 71 | The package also supports `parent -> children` relationships for comments. This means that a comment can `belongTo` another comment. 72 | 73 | ```php 74 | $post->comment('Thanks for commenting!', parent: Comment::find(2)); 75 | ``` 76 | 77 | ## Testing 78 | 79 | ```bash 80 | composer test 81 | ``` 82 | 83 | ## Changelog 84 | 85 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 86 | 87 | ## Contributing 88 | 89 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 90 | 91 | ## Security Vulnerabilities 92 | 93 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 94 | 95 | ## Credits 96 | 97 | - [Ryan Chandler](https://github.com/ryangjchandler) 98 | - [All Contributors](../../contributors) 99 | 100 | ## License 101 | 102 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 103 | --------------------------------------------------------------------------------