File tree Expand file tree Collapse file tree 5 files changed +69
-2
lines changed
Expand file tree Collapse file tree 5 files changed +69
-2
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 77
88class 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);
Original file line number Diff line number Diff line change 44use UniSharp \Categorizable \Test \TestCase ;
55use UniSharp \Categorizable \Test \TestModel ;
66use UniSharp \Categorizable \Models \Category ;
7- use UniSharp \Categorizable \Services \TagService as UnisharpTagService ;
87use Cviebrock \EloquentTaggable \Services \TagService ;
8+ use UniSharp \Categorizable \Services \TagService as UnisharpTagService ;
99
1010class 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}
Original file line number Diff line number Diff line change 11<?php
22namespace UniSharp \Categorizable \Test ;
33
4- use Cviebrock \EloquentTaggable \ServiceProvider ;
54use Orchestra \Testbench \TestCase as Orchestra ;
5+ use Cviebrock \EloquentTaggable \ServiceProvider ;
6+ use UniSharp \Categorizable \Providers \CategorizableServiceProvider ;
67
78abstract 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 }
You can’t perform that action at this time.
0 commit comments