DatabaseSeeder.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\Submission;
  4. use App\Models\Comment;
  5. use App\Models\Tag;
  6. use App\Models\Attachment;
  7. use Illuminate\Database\Seeder;
  8. class DatabaseSeeder extends Seeder
  9. {
  10. public function run(): void
  11. {
  12. // Создаем теги
  13. $tags = collect([
  14. Tag::create(['name' => 'Важное', 'slug' => 'important']),
  15. Tag::create(['name' => 'Срочно', 'slug' => 'urgent']),
  16. Tag::create(['name' => 'Вопрос', 'slug' => 'question']),
  17. Tag::create(['name' => 'Отзыв', 'slug' => 'feedback']),
  18. Tag::create(['name' => 'Проблема', 'slug' => 'issue']),
  19. ]);
  20. // Создаем заявки
  21. for ($i = 1; $i <= 10; $i++) {
  22. $submission = Submission::create([
  23. 'name' => 'Пользователь ' . $i,
  24. 'email' => 'user' . $i . '@example.com',
  25. 'message' => 'Это тестовое сообщение #' . $i . '. Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
  26. 'ip_address' => '192.168.1.' . $i,
  27. 'status' => ['active', 'archived', 'pending'][array_rand(['active', 'archived', 'pending'])],
  28. ]);
  29. // Прикрепляем случайные теги (многие-ко-многим)
  30. $submission->tags()->attach(
  31. $tags->random(rand(1, 3))->pluck('id')->toArray()
  32. );
  33. // Добавляем комментарии (один-ко-многим)
  34. for ($j = 1; $j <= rand(1, 3); $j++) {
  35. $comment = Comment::create([
  36. 'submission_id' => $submission->id,
  37. 'author' => 'Комментатор ' . $j,
  38. 'content' => 'Это комментарий #' . $j . ' к заявке #' . $i,
  39. ]);
  40. // Добавляем вложение к комментарию (полиморфная связь)
  41. if (rand(0, 1)) {
  42. Attachment::create([
  43. 'attachable_type' => Comment::class,
  44. 'attachable_id' => $comment->id,
  45. 'filename' => 'comment_attachment_' . $j . '.pdf',
  46. 'filepath' => '/storage/attachments/comment_' . $comment->id . '.pdf',
  47. 'mime_type' => 'application/pdf',
  48. 'size' => rand(1000, 50000),
  49. ]);
  50. }
  51. }
  52. // Добавляем вложения к заявке (полиморфная связь)
  53. if (rand(0, 1)) {
  54. Attachment::create([
  55. 'attachable_type' => Submission::class,
  56. 'attachable_id' => $submission->id,
  57. 'filename' => 'submission_file_' . $i . '.jpg',
  58. 'filepath' => '/storage/attachments/submission_' . $submission->id . '.jpg',
  59. 'mime_type' => 'image/jpeg',
  60. 'size' => rand(50000, 200000),
  61. ]);
  62. }
  63. }
  64. $this->command->info('База данных успешно заполнена тестовыми данными!');
  65. $this->command->info('Создано:');
  66. $this->command->info('- Заявок: ' . Submission::count());
  67. $this->command->info('- Комментариев: ' . Comment::count());
  68. $this->command->info('- Тегов: ' . Tag::count());
  69. $this->command->info('- Вложений: ' . Attachment::count());
  70. }
  71. }