Is this a Model? or Controller? if so, Apply Scopes on Model, and use Query Builder, for more code readability and reusability.