Skip to content

Commit

Permalink
handle UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrNRD committed Dec 25, 2020
1 parent 101e81f commit 992493a
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
phpunit.xml
vendor
/.history
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 13 additions & 8 deletions config/commentable.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
<?php

/*
/**
* This file is part of Laravel Commentable.
*
* (c) Brian Faust <hello@basecode.sh>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [

/*
Expand All @@ -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,
];
41 changes: 41 additions & 0 deletions database/migrations/2020_05_21_104845_create_comments_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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) {
if(config('commentable.allow_uuid')){
$table->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');
}
}
36 changes: 0 additions & 36 deletions database/migrations/create_comments_table.php

This file was deleted.

6 changes: 4 additions & 2 deletions src/CommentableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
15 changes: 5 additions & 10 deletions src/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
<?php

declare(strict_types=1);

/*
* This file is part of Laravel Commentable.
*
*/

namespace AmrNRD\Commentable\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Kalnoy\Nestedset\NodeTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use AmrNRD\Commentable\Traits\UsesUuid;

class Comment extends Model
{
use NodeTrait;
use NodeTrait, UsesUuid;

/**
* The attributes that are not mass assignable.
Expand Down Expand Up @@ -76,7 +71,7 @@ public function createComment(Model $commentable, array $data): self
*/
public function updateComment(int $id, array $data): bool
{
return (bool) static::find($id)->update($data);
return (bool) static::findOrFail($id)->update($data);
}

/**
Expand All @@ -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();
}
}
37 changes: 37 additions & 0 deletions src/Requests/CustomFieldFormRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace AmrNRD\Commentable\Requests;

use Illuminate\Foundation\Http\FormRequest;
use CustomField;

class CustomFieldFormRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{

$rules = [
'body' => ['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'),
];
}
}
12 changes: 0 additions & 12 deletions src/Traits/HasComments.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
<?php

declare(strict_types=1);

/*
* This file is part of Laravel Commentable.
*
* (c) Brian Faust <hello@basecode.sh>
*
* 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;
Expand Down
50 changes: 50 additions & 0 deletions src/Traits/UsesUUID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace AmrNRD\Commentable\Traits;

use Illuminate\Support\Str;

trait UsesUuid
{

/**
* Customize the model primary key base on config value
*
* @return bool
*/
public function getIncrementing()
{
if(config('cf.allow_uuid')){
return false;
}

return true;
}

/**
* Get Key Type
*
* @return string
*/
public function getKeyType()
{
if(config('cf.allow_uuid')){
return 'string';
}
return 'int';
}

/**
* Setup Saving Boot
*
* @return void
*/
protected static function bootUsesUuid()
{
static::creating(function ($model) {
if (!$model->getKey() && config('cf.allow_uuid')) {
$model->{$model->getKeyName()} = (string) Str::uuid();
}
});
}
}

0 comments on commit 992493a

Please sign in to comment.