AiModelFactory.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Database\Factories;
  3. use App\Models\AiModel;
  4. use Illuminate\Database\Eloquent\Factories\Factory;
  5. use Illuminate\Support\Str;
  6. /**
  7. * @extends Factory<AiModel>
  8. */
  9. class AiModelFactory extends Factory
  10. {
  11. protected $model = AiModel::class;
  12. /**
  13. * Define the model's default state.
  14. *
  15. * @return array<string, mixed>
  16. */
  17. public function definition(): array
  18. {
  19. return [
  20. 'uid' => (string) Str::uuid(),
  21. 'name' => fake()->unique()->slug(2),
  22. // real_name 是模型的登录标识,表上有 unique 约束
  23. 'real_name' => (string) Str::uuid(),
  24. 'description' => fake()->sentence(),
  25. 'url' => 'https://api.example.com',
  26. 'model' => 'gpt-4',
  27. 'key' => 'sk-'.fake()->uuid(),
  28. 'system_prompt' => 'you are a helpful assistant',
  29. 'privacy' => 'private',
  30. 'owner_id' => (string) Str::uuid(),
  31. 'editor_id' => (string) Str::uuid(),
  32. ];
  33. }
  34. /**
  35. * AiModel 没有声明 $fillable(默认 guarded = ['*']),构造器里 fill() 会抛
  36. * MassAssignmentException。这里绕开批量赋值保护,而不是为了测试去放开生产模型的写入面。
  37. */
  38. public function newModel(array $attributes = [])
  39. {
  40. $model = $this->modelName();
  41. return (new $model)->forceFill($attributes);
  42. }
  43. /**
  44. * 归属于指定 studio(个人 studio 即用户 uid)。
  45. */
  46. public function ownedBy(string $ownerId): static
  47. {
  48. return $this->state(fn (array $attributes) => [
  49. 'owner_id' => $ownerId,
  50. 'editor_id' => $ownerId,
  51. ]);
  52. }
  53. }