Skip to content

Commit e013134

Browse files
author
FreedomKnight
committed
加上 parent, children
1 parent 20fceda commit e013134

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class UpdateTaggableTable extends Migration
8+
{
9+
10+
/**
11+
* Run the migrations.
12+
*
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
$connection = config('taggable.connection');
18+
19+
Schema::connection($connection)->table('taggable_tags', function (Blueprint $table) {
20+
$table->unsignedInteger('parent_id')->nullable();
21+
$table->foreign('parent_id')->references('id')->on('taggable_tags')->onDelete('null');
22+
$table->index('parent_id');
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
$connection = config('taggable.connection');
34+
35+
Schema::connection($connection)->table('taggable_tags', function (Blueprint $table) {
36+
$table->dropForeign('taggable_tags_parent_id_foreign');
37+
});
38+
}
39+
}

src/Models/Category.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,14 @@ class Category extends Tag
88
{
99
//
1010
protected $table = 'taggable_tags';
11+
12+
public function parent()
13+
{
14+
return $this->belongsTo(Category::class, 'parent_id');
15+
}
16+
17+
public function children()
18+
{
19+
return $this->hasMany(Category::class, 'parent_id');
20+
}
1121
}

src/Providers/CategorizableServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
class CategorizableServiceProvider extends ServiceProvider
99
{
10+
public function boot()
11+
{
12+
$this->loadMigrationsFrom(
13+
__DIR__.'/../../database/migrations'
14+
);
15+
}
1016
public function register()
1117
{
1218
$this->app->singleton(TagService::class, UnisharpTagService::class);

tests/CategoryTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use UniSharp\Categorizable\Test\TestCase;
55
use UniSharp\Categorizable\Test\TestModel;
66
use UniSharp\Categorizable\Models\Category;
7-
use UniSharp\Categorizable\Services\TagService as UnisharpTagService;
87
use Cviebrock\EloquentTaggable\Services\TagService;
8+
use UniSharp\Categorizable\Services\TagService as UnisharpTagService;
99

1010
class CategoriesTest extends TestCase
1111
{
@@ -96,4 +96,14 @@ public function testCategoryDeTagId()
9696
$this->testModel->tagList
9797
);
9898
}
99+
100+
public function testAssociateParent()
101+
{
102+
$parent = Category::create(['name' => 'parent']);
103+
$child = Category::create(['name' => 'child']);
104+
105+
$child->parent()->associate($parent)->save();
106+
107+
$this->assertEquals('child', $parent->refresh()->children->first()->name);
108+
}
99109
}

tests/TestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
namespace UniSharp\Categorizable\Test;
33

4-
use Cviebrock\EloquentTaggable\ServiceProvider;
54
use Orchestra\Testbench\TestCase as Orchestra;
5+
use Cviebrock\EloquentTaggable\ServiceProvider;
6+
use UniSharp\Categorizable\Providers\CategorizableServiceProvider;
67

78
abstract class TestCase extends Orchestra
89
{
@@ -32,6 +33,7 @@ protected function getPackageProviders($app)
3233
{
3334
return [
3435
ServiceProvider::class,
36+
CategorizableServiceProvider::class,
3537
TestServiceProvider::class,
3638
];
3739
}

0 commit comments

Comments
 (0)