{"id":33957203,"url":"https://github.com/ahoysolutions/query-filters","last_synced_at":"2026-04-19T20:32:35.719Z","repository":{"id":56942590,"uuid":"96918674","full_name":"ahoysolutions/query-filters","owner":"ahoysolutions","description":"A Laravel package to filter database queries based on the query string.","archived":false,"fork":false,"pushed_at":"2017-07-31T19:14:59.000Z","size":25,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-14T09:40:23.065Z","etag":null,"topics":["filter","laravel","query-string"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ahoysolutions.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-11T17:22:15.000Z","updated_at":"2023-11-27T21:07:53.000Z","dependencies_parsed_at":"2022-08-21T02:10:18.369Z","dependency_job_id":null,"html_url":"https://github.com/ahoysolutions/query-filters","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/ahoysolutions/query-filters","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoysolutions%2Fquery-filters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoysolutions%2Fquery-filters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoysolutions%2Fquery-filters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoysolutions%2Fquery-filters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahoysolutions","download_url":"https://codeload.github.com/ahoysolutions/query-filters/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoysolutions%2Fquery-filters/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32022041,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T20:23:30.271Z","status":"online","status_checked_at":"2026-04-19T02:00:07.110Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filter","laravel","query-string"],"created_at":"2025-12-12T20:58:52.826Z","updated_at":"2026-04-19T20:32:35.713Z","avatar_url":"https://github.com/ahoysolutions.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Query Filters\r\n\r\n## Installation\r\n\r\n### Step 1: Composer\r\nFrom the command line, run:\r\n```\r\ncomposer require ahoysolutions/query-filters\r\n```\r\n\r\n### Step 2: Service Provider\r\nFrom within your Laravel application, open `config/app.php` and within the `providers` array, add:\r\n\r\n```\r\nAhoySolutions\\QueryFilters\\QueryFilterServiceProvider::class\r\n```\r\nThis will bootstrap the package into Laravel for you.\r\n\r\n## Usage\r\n\r\n### Adding a filters class to a model\r\nYou can add a filter class through artisan command, just like with controllers, models, or other similar resources.  For example, assuming you wanted to leverage filters on your Post model, you might use:\r\n\r\n```\r\nphp artisan make:queryfilters PostFilters\r\n```\r\n\r\nAfterwards, a new query filter class will be added to your ```app/Filters/``` directory.\r\n\r\n### Adding filter methods\r\nTo add a filter method to an filter class, simply add a function to the class. For example, assume you have an incoming request with a query string that looks like ```www.example.com/posts?user=johnsmith\u0026popular```.\r\n\r\nYour filters class might then look like this: \r\n\r\n```\r\n\u003c?php\r\n\r\nnamespace App\\Filters;\r\n\r\nuse AhoySolutions\\QueryFilters\\QueryFilters;\r\nuse App\\User;\r\n\r\nclass PostFilters extends QueryFilters\r\n{\r\n    /**\r\n    * Filter the posts by the given user.\r\n    *\r\n    * @param string $username\r\n    * @return \\Illuminate\\Database\\Eloquent\\Builder\r\n    */\r\n    protected function filterUser(string $username)\r\n    {\r\n        $user = User::where('username', $username)-\u003efirstOrFail();\r\n\r\n        return $this-\u003ebuilder-\u003ewhere('artist_id', $user-\u003eid);\r\n    }\r\n\r\n    /**\r\n     * Filter the posts by their popularity.\r\n     *\r\n     * @return \\Illuminate\\Database\\Eloquent\\Builder\r\n     */\r\n    protected function filterPopular()\r\n    {\r\n        $this-\u003eresetOrderBy();\r\n\r\n        return $this-\u003ebuilder-\u003ewithCount('favorites')-\u003eorderBy('favorites_count', 'desc');\r\n    }\r\n}\r\n```\r\n\r\n### Filtering for an query string array\r\nImagine your user wants to search based on an array of different tags, for example:\r\n\r\n```\r\n// given url: /posts?tag[]=science\u0026tag[]=music\r\n\r\n/**\r\n * Filter the posts by a given tag.\r\n *\r\n * @param string $tag\r\n * @return \\Illuminate\\Database\\Eloquent\\Builder\r\n */\r\nprotected function filterTag(string $tag)\r\n{\r\n    return $this-\u003ebuilder-\u003ewhereHas('tags', function ($query) use ($tag) {\r\n       return $query-\u003ewhere('name', $tag);\r\n   });;\r\n}\r\n```\r\nAs you can see, you don't have to do anything within the filter to allow the user to leverage this functionality.\r\n\r\nThe relevant query filter will then be called multiple times and apply the filter for each tag.  This is good for checking against many-to-many relationships.\r\n\r\n### Adding sortable methods\r\nYou can specify that a field should be sortable by calling `$this-\u003eresetOrderBy()` before leveraging the builder, for example:\r\n\r\n```\r\n// given url: /resource?popular\r\n\r\n/**\r\n * Sorts the posts by their popularity.\r\n *\r\n * @return \\Illuminate\\Database\\Eloquent\\Builder\r\n */\r\nprotected function filterPopular()\r\n{\r\n    $this-\u003eresetOrderBy();\r\n\r\n    return $this-\u003ebuilder-\u003ewithCount('favorites')-\u003eorderBy('favorites_count', 'desc');\r\n}\r\n```\r\n\r\nIf you have multiple sortables, the latest one leveraged by the user will take precedence.\r\n\r\n## Credits\r\n* Inspired by [Laracasts](https://laracasts.com) (https://laracasts.com/series/lets-build-a-forum-with-laravel)\r\n* Originally developed by [Chris Sorrells](https://www.chrissorrells.com/) on behalf of [Ahoy Solutions, LLC](https://www.ahoysolutions.com).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahoysolutions%2Fquery-filters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahoysolutions%2Fquery-filters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahoysolutions%2Fquery-filters/lists"}