diff --git a/.gitignore b/.gitignore index 81b9258..2cbbafb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock phpunit.xml vendor +/.history diff --git a/README.md b/README.md index 217da60..da16d58 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Require this package, with [Composer](https://getcomposer.org/), in the root directory of your project. ``` bash -$ composer require amrnrd/commentable +$ composer require AmrNRD/commentable ``` To get started, you'll need to publish the vendor assets and migrate: diff --git a/config/commentable.php b/config/commentable.php index dff1181..69559d2 100644 --- a/config/commentable.php +++ b/config/commentable.php @@ -1,14 +1,8 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. */ - return [ /* @@ -20,5 +14,16 @@ | */ 'model' => \AmrNRD\Commentable\Models\Comment::class, - 'user' => \App\Domains\User\Entities\User::class + 'user' => \App\Domains\User\Entities\User::class, + + + /* + |-------------------------------------------------------------------------- + | Allow UUID + |-------------------------------------------------------------------------- + | + | This option change the id to UUID + | + */ + 'allow_uuid' => false, ]; diff --git a/database/migrations/2020_05_21_104845_create_comments_table.php b/database/migrations/2020_05_21_104845_create_comments_table.php new file mode 100644 index 0000000..4fe04fb --- /dev/null +++ b/database/migrations/2020_05_21_104845_create_comments_table.php @@ -0,0 +1,41 @@ +uuid('id')->primary(); + $table->uuidMorphs('commentable'); + $table->uuid('user_id'); + + // $table->nestedSet() + $table->uuid('parent_id')->nullable(); + }else{ + $table->id('id'); + $table->morphs('commentable'); + $table->unsignedBigInteger('user_id'); + + // $table->nestedSet(); + $table->unsignedBigInteger('parent_id')->nullable(); + } + + $table->unsignedInteger('_lft')->default(0); + $table->unsignedInteger('_rgt')->default(0); + + $table->text('body'); + + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('comments'); + } +} diff --git a/database/migrations/create_comments_table.php b/database/migrations/create_comments_table.php deleted file mode 100644 index 30f7d7b..0000000 --- a/database/migrations/create_comments_table.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -use Illuminate\Database\Migrations\Migration; -use Illuminate\Database\Schema\Blueprint; -use Kalnoy\Nestedset\NestedSet; - -class CreateCommentsTable extends Migration -{ - public function up() - { - Schema::create('comments', function (Blueprint $table) { - $table->id('id'); - $table->text('body'); - $table->morphs('commentable'); - $table->unsignedBigInteger('user_id'); - $table->nestedSet(); - $table->timestamps(); - }); - } - - public function down() - { - Schema::dropIfExists('comments'); - } -} diff --git a/src/CommentableServiceProvider.php b/src/CommentableServiceProvider.php index a6847ed..d030cdf 100644 --- a/src/CommentableServiceProvider.php +++ b/src/CommentableServiceProvider.php @@ -22,13 +22,15 @@ class CommentableServiceProvider extends ServiceProvider */ public function boot() { + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); + $this->publishes([ __DIR__.'/../database/migrations/create_comments_table.php' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_comments_table.php'), - ], 'migrations'); + ], 'commentable_migrations'); $this->publishes([ __DIR__.'/../config/commentable.php' => config_path('commentable.php'), - ], 'config'); + ], 'commentable_config'); } /** diff --git a/src/Models/Comment.php b/src/Models/Comment.php index aba2c58..75384d2 100644 --- a/src/Models/Comment.php +++ b/src/Models/Comment.php @@ -1,21 +1,16 @@ update($data); + return (bool) static::findOrFail($id)->update($data); } /** @@ -88,6 +83,6 @@ public function updateComment(int $id, array $data): bool */ public function deleteComment(int $id): bool { - return (bool) static::find($id)->delete(); + return (bool) static::findOrFail($id)->delete(); } } diff --git a/src/Requests/CustomFieldFormRequest.php b/src/Requests/CustomFieldFormRequest.php new file mode 100644 index 0000000..a90f0bd --- /dev/null +++ b/src/Requests/CustomFieldFormRequest.php @@ -0,0 +1,37 @@ + ['required', 'string', 'max:255'], + 'parent_id' => ['nullable', 'numeric', 'exists:comments,id'], + ]; + return $rules; + } + + /** + * Get custom attributes for validator errors. + * + * @return array + */ + public function attributes() + { + return [ + 'body' => __('main.label'), + 'parent_id' => __('main.name'), + ]; + } +} diff --git a/src/Traits/HasComments.php b/src/Traits/HasComments.php index 91a6b02..f94dba7 100644 --- a/src/Traits/HasComments.php +++ b/src/Traits/HasComments.php @@ -1,16 +1,4 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace AmrNRD\Commentable\Traits; use AmrNRD\Commentable\Models\Comment; diff --git a/src/Traits/UsesUUID.php b/src/Traits/UsesUUID.php new file mode 100644 index 0000000..ff8793c --- /dev/null +++ b/src/Traits/UsesUUID.php @@ -0,0 +1,50 @@ +getKey() && config('cf.allow_uuid')) { + $model->{$model->getKeyName()} = (string) Str::uuid(); + } + }); + } +}