|
9 | 9 |
|
10 | 10 | class PostSeeder extends Seeder |
11 | 11 | { |
| 12 | + /** |
| 13 | + * 1~5行のランダムなcontentを生成 |
| 14 | + */ |
| 15 | + private function generateContent(int $lineCount, string $userName, int $postNumber, string $type = '投稿'): string |
| 16 | + { |
| 17 | + $lines = []; |
| 18 | + for ($i = 1; $i <= $lineCount; $i++) { |
| 19 | + $lines[] = "これは{$userName}の{$type} {$postNumber} の{$i}行目です。"; |
| 20 | + } |
| 21 | + return implode("\n", $lines); |
| 22 | + } |
| 23 | + |
12 | 24 | public function run(): void |
13 | 25 | { |
14 | | - $paidUser = User::where('email', 'paid@example.com')->first(); |
| 26 | + $paidUser = User::where('email', 'watanabe@example.com')->first(); |
| 27 | + $paidUser2 = User::where('email', 'matsumoto@example.com')->first(); |
15 | 28 | $adminUser = User::where('email', 'admin@example.com')->first(); |
16 | 29 |
|
17 | | - // paidユーザーに 5 投稿 |
| 30 | + // paidユーザー(わたなべ)に 5 投稿(公開) |
18 | 31 | if ($paidUser) { |
19 | 32 | for ($i = 1; $i <= 5; $i++) { |
| 33 | + $lineCount = rand(1, 5); |
| 34 | + Post::create([ |
| 35 | + 'user_id' => $paidUser->id, |
| 36 | + 'title' => "わたなべの投稿 {$i}", |
| 37 | + 'content' => $this->generateContent($lineCount, 'わたなべ', $i, '投稿'), |
| 38 | + 'status' => 'published', |
| 39 | + ]); |
| 40 | + } |
| 41 | + // paidユーザー(わたなべ)に 3 下書き投稿 |
| 42 | + for ($i = 1; $i <= 3; $i++) { |
| 43 | + $lineCount = rand(1, 5); |
20 | 44 | Post::create([ |
21 | 45 | 'user_id' => $paidUser->id, |
22 | | - 'title' => "Paidユーザーの投稿 {$i}", |
23 | | - 'content' => "これはPaidユーザーの投稿 {$i} です。", |
| 46 | + 'title' => "わたなべの下書き {$i}", |
| 47 | + 'content' => $this->generateContent($lineCount, 'わたなべ', $i, '下書き'), |
| 48 | + 'status' => 'draft', |
| 49 | + ]); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // paidユーザー2(まつもと)に 5 投稿(公開) |
| 54 | + if ($paidUser2) { |
| 55 | + for ($i = 1; $i <= 5; $i++) { |
| 56 | + $lineCount = rand(1, 5); |
| 57 | + Post::create([ |
| 58 | + 'user_id' => $paidUser2->id, |
| 59 | + 'title' => "まつもとの投稿 {$i}", |
| 60 | + 'content' => $this->generateContent($lineCount, 'まつもと', $i, '投稿'), |
24 | 61 | 'status' => 'published', |
25 | 62 | ]); |
26 | 63 | } |
| 64 | + // paidユーザー2(まつもと)に 3 下書き投稿 |
| 65 | + for ($i = 1; $i <= 3; $i++) { |
| 66 | + $lineCount = rand(1, 5); |
| 67 | + Post::create([ |
| 68 | + 'user_id' => $paidUser2->id, |
| 69 | + 'title' => "まつもとの下書き {$i}", |
| 70 | + 'content' => $this->generateContent($lineCount, 'まつもと', $i, '下書き'), |
| 71 | + 'status' => 'draft', |
| 72 | + ]); |
| 73 | + } |
27 | 74 | } |
28 | 75 |
|
29 | | - // adminユーザーに 3 投稿 |
| 76 | + // adminユーザーに 3 投稿(公開) |
30 | 77 | if ($adminUser) { |
31 | 78 | for ($i = 1; $i <= 3; $i++) { |
| 79 | + $lineCount = rand(1, 5); |
32 | 80 | Post::create([ |
33 | 81 | 'user_id' => $adminUser->id, |
34 | 82 | 'title' => "Adminユーザーの投稿 {$i}", |
35 | | - 'content' => "これはAdminユーザーの投稿 {$i} です。", |
| 83 | + 'content' => $this->generateContent($lineCount, 'Adminユーザー', $i, '投稿'), |
36 | 84 | 'status' => 'published', |
37 | 85 | ]); |
38 | 86 | } |
| 87 | + // adminユーザーに 2 下書き投稿 |
| 88 | + for ($i = 1; $i <= 2; $i++) { |
| 89 | + $lineCount = rand(1, 5); |
| 90 | + Post::create([ |
| 91 | + 'user_id' => $adminUser->id, |
| 92 | + 'title' => "Adminユーザーの下書き {$i}", |
| 93 | + 'content' => $this->generateContent($lineCount, 'Adminユーザー', $i, '下書き'), |
| 94 | + 'status' => 'draft', |
| 95 | + ]); |
| 96 | + } |
39 | 97 | } |
40 | 98 | } |
41 | 99 | } |
|
0 commit comments