feat(阶段三): 添加知识库模型和迁移
- 创建 knowledge_bases 表迁移 - 创建 KnowledgeBase 模型 - 创建 KnowledgeBaseFactory 工厂类 - 支持与终端的多对多关联关系
This commit is contained in:
36
app/Models/KnowledgeBase.php
Normal file
36
app/Models/KnowledgeBase.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class KnowledgeBase extends Model
|
||||||
|
{
|
||||||
|
use HasFactory, SoftDeletes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可批量赋值的属性
|
||||||
|
*
|
||||||
|
* @var array<string>
|
||||||
|
*/
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
48
database/factories/KnowledgeBaseFactory.php
Normal file
48
database/factories/KnowledgeBaseFactory.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\KnowledgeBase;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\KnowledgeBase>
|
||||||
|
*/
|
||||||
|
class KnowledgeBaseFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = KnowledgeBase::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
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',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('knowledge_bases', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user