Use scopes for reusable queries:
Local Scopes
public function scopePopular($query)
{
return $query->where('views', '>', 1000);
}Using Scopes
$posts = Post::popular()->published()->get();Dynamic Scopes
public function scopeOfType($query, $type)
{
return $query->where('type', $type);
}Global Scopes
class ActiveScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('active', 1);
}
}