public function run() { DB::table('articles')->delete(); factory(Article::class, 250)->make()->each(function ($article) { $article->author()->associate(User::all()->random(1)); $article->category()->associate(Category::all()->random(1)); $article->save(); $tags = Tag::all()->random(rand(1, 10)); if ($tags instanceof Collection) { foreach ($tags as $tag) { $article->tags()->attach($tag->id); } } else { $article->tags()->attach($tags->id); } }); }