Skip to content

Commit

Permalink
加上 parent, children
Browse files Browse the repository at this point in the history
  • Loading branch information
FreedomKnight committed Feb 9, 2018
1 parent 20fceda commit e013134
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
39 changes: 39 additions & 0 deletions database/migrations/0000_00_00_000000_update_taggable_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateTaggableTable extends Migration
{

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$connection = config('taggable.connection');

Schema::connection($connection)->table('taggable_tags', function (Blueprint $table) {
$table->unsignedInteger('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('taggable_tags')->onDelete('null');
$table->index('parent_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$connection = config('taggable.connection');

Schema::connection($connection)->table('taggable_tags', function (Blueprint $table) {
$table->dropForeign('taggable_tags_parent_id_foreign');
});
}
}
10 changes: 10 additions & 0 deletions src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ class Category extends Tag
{
//
protected $table = 'taggable_tags';

public function parent()
{
return $this->belongsTo(Category::class, 'parent_id');
}

public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
}
6 changes: 6 additions & 0 deletions src/Providers/CategorizableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class CategorizableServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadMigrationsFrom(
__DIR__.'/../../database/migrations'
);
}
public function register()
{
$this->app->singleton(TagService::class, UnisharpTagService::class);
Expand Down
12 changes: 11 additions & 1 deletion tests/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use UniSharp\Categorizable\Test\TestCase;
use UniSharp\Categorizable\Test\TestModel;
use UniSharp\Categorizable\Models\Category;
use UniSharp\Categorizable\Services\TagService as UnisharpTagService;
use Cviebrock\EloquentTaggable\Services\TagService;
use UniSharp\Categorizable\Services\TagService as UnisharpTagService;

class CategoriesTest extends TestCase
{
Expand Down Expand Up @@ -96,4 +96,14 @@ public function testCategoryDeTagId()
$this->testModel->tagList
);
}

public function testAssociateParent()
{
$parent = Category::create(['name' => 'parent']);
$child = Category::create(['name' => 'child']);

$child->parent()->associate($parent)->save();

$this->assertEquals('child', $parent->refresh()->children->first()->name);
}
}
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
namespace UniSharp\Categorizable\Test;

use Cviebrock\EloquentTaggable\ServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use Cviebrock\EloquentTaggable\ServiceProvider;
use UniSharp\Categorizable\Providers\CategorizableServiceProvider;

abstract class TestCase extends Orchestra
{
Expand Down Expand Up @@ -32,6 +33,7 @@ protected function getPackageProviders($app)
{
return [
ServiceProvider::class,
CategorizableServiceProvider::class,
TestServiceProvider::class,
];
}
Expand Down

0 comments on commit e013134

Please sign in to comment.