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); } }); }
/** * Get Tags from the database * @param $filters * @return static */ public function get($filters = null) { $quantity = isset($filters['quantity']) ? $filters['quantity'] : 10; return Tag::all()->take($quantity); }