Skip to content

Commit 985bb50

Browse files
committed
feat(articles-seeder): added multiple co authors seeding
1 parent f282c52 commit 985bb50

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

database/seeders/ArticleCommentSeeder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public function run(): void
2828
$articles = Article::factory(100)->create();
2929

3030
foreach ($articles as $article) {
31+
// Add 0-3 authors to the authors relation, excluding the main author/created_by
32+
$possibleAuthors = $users->where('id', '!=', $article->created_by);
33+
$authorCount = rand(0, 3);
34+
if ($authorCount > 0 && $possibleAuthors->count() > 0) {
35+
$authorIds = $possibleAuthors->random(min($authorCount, $possibleAuthors->count()))->pluck('id')->toArray();
36+
$article->authors()->attach($authorIds);
37+
}
3138
// Attach 1-3 random categories to each article
3239
$article->categories()->attach($categories->random(rand(1, 3))->pluck('id')->toArray());
3340
// Attach 2-5 random tags to each article

0 commit comments

Comments
 (0)