From 333034d2f11e259368bf392b9478581dbfbdc83b Mon Sep 17 00:00:00 2001 From: lizhuoran <625237490@qq.com> Date: Mon, 9 Mar 2026 10:59:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=98=B6=E6=AE=B5=E4=B8=89):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=9F=A5=E8=AF=86=E5=BA=93=E6=A8=A1=E5=9E=8B=E5=92=8C?= =?UTF-8?q?=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 knowledge_bases 表迁移 - 创建 KnowledgeBase 模型 - 创建 KnowledgeBaseFactory 工厂类 - 支持与终端的多对多关联关系 --- app/Models/KnowledgeBase.php | 36 ++++++++++++++ database/factories/KnowledgeBaseFactory.php | 48 +++++++++++++++++++ ...09_022236_create_knowledge_bases_table.php | 31 ++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 app/Models/KnowledgeBase.php create mode 100644 database/factories/KnowledgeBaseFactory.php create mode 100644 database/migrations/2026_03_09_022236_create_knowledge_bases_table.php diff --git a/app/Models/KnowledgeBase.php b/app/Models/KnowledgeBase.php new file mode 100644 index 0000000..ae19946 --- /dev/null +++ b/app/Models/KnowledgeBase.php @@ -0,0 +1,36 @@ + + */ + protected $fillable = [ + 'name', + 'description', + 'status', + ]; + + /** + * 获取关联的终端 + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function terminals() + { + return $this->belongsToMany(Terminal::class, 'terminal_knowledge_bases') + ->withPivot('priority') + ->withTimestamps() + ->orderBy('priority'); + } +} diff --git a/database/factories/KnowledgeBaseFactory.php b/database/factories/KnowledgeBaseFactory.php new file mode 100644 index 0000000..0ae9e4a --- /dev/null +++ b/database/factories/KnowledgeBaseFactory.php @@ -0,0 +1,48 @@ + + */ +class KnowledgeBaseFactory extends Factory +{ + protected $model = KnowledgeBase::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->words(3, true), + 'description' => $this->faker->sentence(), + 'status' => $this->faker->randomElement(['active', 'inactive']), + ]; + } + + /** + * 指定知识库为激活状态 + */ + public function active(): static + { + return $this->state(fn (array $attributes) => [ + 'status' => 'active', + ]); + } + + /** + * 指定知识库为非激活状态 + */ + public function inactive(): static + { + return $this->state(fn (array $attributes) => [ + 'status' => 'inactive', + ]); + } +} diff --git a/database/migrations/2026_03_09_022236_create_knowledge_bases_table.php b/database/migrations/2026_03_09_022236_create_knowledge_bases_table.php new file mode 100644 index 0000000..dd7471e --- /dev/null +++ b/database/migrations/2026_03_09_022236_create_knowledge_bases_table.php @@ -0,0 +1,31 @@ +id(); + $table->string('name')->comment('知识库名称'); + $table->text('description')->nullable()->comment('知识库描述'); + $table->string('status')->default('active')->comment('状态'); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('knowledge_bases'); + } +};