| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace Database\Seeders;
- use App\Models\Submission;
- use App\Models\Comment;
- use App\Models\Tag;
- use App\Models\Attachment;
- use Illuminate\Database\Seeder;
- class DatabaseSeeder extends Seeder
- {
- public function run(): void
- {
- // Создаем теги
- $tags = collect([
- Tag::create(['name' => 'Важное', 'slug' => 'important']),
- Tag::create(['name' => 'Срочно', 'slug' => 'urgent']),
- Tag::create(['name' => 'Вопрос', 'slug' => 'question']),
- Tag::create(['name' => 'Отзыв', 'slug' => 'feedback']),
- Tag::create(['name' => 'Проблема', 'slug' => 'issue']),
- ]);
- // Создаем заявки
- for ($i = 1; $i <= 10; $i++) {
- $submission = Submission::create([
- 'name' => 'Пользователь ' . $i,
- 'email' => 'user' . $i . '@example.com',
- 'message' => 'Это тестовое сообщение #' . $i . '. Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
- 'ip_address' => '192.168.1.' . $i,
- 'status' => ['active', 'archived', 'pending'][array_rand(['active', 'archived', 'pending'])],
- ]);
- // Прикрепляем случайные теги (многие-ко-многим)
- $submission->tags()->attach(
- $tags->random(rand(1, 3))->pluck('id')->toArray()
- );
- // Добавляем комментарии (один-ко-многим)
- for ($j = 1; $j <= rand(1, 3); $j++) {
- $comment = Comment::create([
- 'submission_id' => $submission->id,
- 'author' => 'Комментатор ' . $j,
- 'content' => 'Это комментарий #' . $j . ' к заявке #' . $i,
- ]);
- // Добавляем вложение к комментарию (полиморфная связь)
- if (rand(0, 1)) {
- Attachment::create([
- 'attachable_type' => Comment::class,
- 'attachable_id' => $comment->id,
- 'filename' => 'comment_attachment_' . $j . '.pdf',
- 'filepath' => '/storage/attachments/comment_' . $comment->id . '.pdf',
- 'mime_type' => 'application/pdf',
- 'size' => rand(1000, 50000),
- ]);
- }
- }
- // Добавляем вложения к заявке (полиморфная связь)
- if (rand(0, 1)) {
- Attachment::create([
- 'attachable_type' => Submission::class,
- 'attachable_id' => $submission->id,
- 'filename' => 'submission_file_' . $i . '.jpg',
- 'filepath' => '/storage/attachments/submission_' . $submission->id . '.jpg',
- 'mime_type' => 'image/jpeg',
- 'size' => rand(50000, 200000),
- ]);
- }
- }
- $this->command->info('База данных успешно заполнена тестовыми данными!');
- $this->command->info('Создано:');
- $this->command->info('- Заявок: ' . Submission::count());
- $this->command->info('- Комментариев: ' . Comment::count());
- $this->command->info('- Тегов: ' . Tag::count());
- $this->command->info('- Вложений: ' . Attachment::count());
- }
- }
|