File tree Expand file tree Collapse file tree 1 file changed +7
-0
lines changed Expand file tree Collapse file tree 1 file changed +7
-0
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments