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); } }); }
public function scopeFindCategory($query, $name) { return $query->where('category_id', Category::where('slug', $name)->first()->id); }
private function filter(Builder $builder, $filters) { if (isset($filters['take'])) { $builder->take($filters['take']); } if (isset($filters['category'])) { $builder->where('category_id', Category::where('slug', $filters['category'])->firstOrfail()->id); } if (isset($filters['online']) && $filters['online'] == true) { $builder->online(); } if (isset($filters['order'])) { $builder->orderBy($filters['order'], 'desc'); } else { $builder->latest('published_at'); } return $builder; }