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'); + } +};