{"id":36933758,"url":"https://github.com/jiaxincui/laravel-request-query","last_synced_at":"2026-04-04T12:06:29.782Z","repository":{"id":325942756,"uuid":"1102810050","full_name":"jiaxincui/laravel-request-query","owner":"jiaxincui","description":"Eloquent request filter for laravel","archived":false,"fork":false,"pushed_at":"2026-04-04T10:01:10.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-04T11:35:38.920Z","etag":null,"topics":["eloquent","filter","laravel","query","request"],"latest_commit_sha":null,"homepage":"","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/jiaxincui.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-24T04:26:34.000Z","updated_at":"2026-04-04T10:00:37.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/jiaxincui/laravel-request-query","commit_stats":null,"previous_names":["jiaxincui/laravel-request-query"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/jiaxincui/laravel-request-query","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaxincui%2Flaravel-request-query","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaxincui%2Flaravel-request-query/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaxincui%2Flaravel-request-query/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaxincui%2Flaravel-request-query/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jiaxincui","download_url":"https://codeload.github.com/jiaxincui/laravel-request-query/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaxincui%2Flaravel-request-query/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31398786,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-04T10:20:44.708Z","status":"ssl_error","status_checked_at":"2026-04-04T10:20:06.846Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["eloquent","filter","laravel","query","request"],"created_at":"2026-01-13T09:44:09.563Z","updated_at":"2026-04-04T12:06:29.774Z","avatar_url":"https://github.com/jiaxincui.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## About\n\nEloquent request filter for laravel\n\n## 使用示例\n请求时使用对应的参数返回过滤后的数据\n\n`/api/users?where=age:gt:18`\n\n`/api/users?where=name:like:foo;email:like:bar`\n\n`/api/users?where=age:gt:18\u0026slice=5,10\u0026orderBy=id,desc`\n\n`/api/users?where[]=age:gt:18\u0026where[]=name:foo\u0026slice=5,10\u0026orderBy=id,desc`\n\n## 使用步骤\n\n**1. 在 `Model` 里使用 `trait` 类**\n\n`use Jiaxincui\\RequestFilter\\FilterScope;`\n\n**2. 创建 `Filter` 类**\n\n`php artisan make:filter User`\n\n将生成一个 filter 类 `App\\Filters\\UserFilter`\n\n你还需要显式定义可用于过滤的字段，见后续章节。\n\n**3. 在查询时添加 `Filter` 类**\n\n`User::fitler(new UserFilter)-\u003eget()`\n\n**4. 查询请求**\n\n`/api/users?where=age:gt:18;name:foo`\n\n## 默认可用的方法\n\n### with\n`with` 示例：`with=userinfo`\n\n### orderBy\n`orderBy` (大小写敏感)， 示例：`orderBy=id,desc`\n\n### slice\n`slice` 示例： `slice=5,10`\n\n### trashed\n`trashed` 示例： `trashed=only`、`trashed=with`\n\n### where\n\n`where` 和 `where[]` 示例：`where=age:gt:18`、`where[]=name:like:foo\u0026where[]=age:gt:18`\n\n**操作符**\n\n对于 `where` 一个查询参数分3段，并且使用`:` (冒号) 作为分隔符，分别代表字段、操作符、值\n\n支持的操作符如下：\n\n`eq` `lt` `gt` `lte` `gte` `neq` `in` `notin` `between` `notbetween` `null` `notnull`\n\n**注意**\n\n其中 `null` `notnull` 参数为2段\n\n示例：`where=card_number:null`\n\n**列表项**\n\n列表项使用 `,` 作为分隔符\n\n以下示例展示了分隔符 `,` 的使用\n\n`api/users?where=name:in:foo,bar\u0026with=userinfo,adress\u0026orderBy=id,desc\u0026slice=5,10`\n\n\n## 自定义方法\n\n除了使用默认方法，你还可以在生成的类里自定义方法\n\n```php\n// App\\Filters\\UserFilter\n\npublic function onlyVip($arg){\n    $this-\u003ebuilder-\u003ewhere('vip', '=', $arg)\n}\n```\n使用： `/api/users?onlyVip=1`\n\n## 分组\n\n对于 `where` 查询需要区分 `and` 和 `or` , 以下示例可以告诉你答案 \n\n**请留意 `;` 符号的使用**\n\n**示例1**\n\n`/api/users?where=age:gt:18;name:foo` \n\nSQL `age \u003e 18 or neme = 'foo'`\n\n**示例2**\n\n`/api/users?where[]=age:gt:18\u0026where[]=name:foo`\n\nSQL `age \u003e 18 and neme = 'foo'`\n\n**示例3**\n\n`/api/users?where[]=age:lte:18;vip:eq:1\u0026where[]=name:foo`\n\nSQL `(age \u003e 18 or vip = 1) and neme = 'foo'`\n\n## 关联查询\n\n基本\n\n`/api/users?with=userinfo` 等同于 `User::with('userinfo')-\u003eget();`\n\n带条件的关联查询\n\n`/api/users?with=userinfo\u0026where=userinfo.age:gt:18` 等同于 `User::whereHas('userinfo', fn ($query) =\u003e $query-\u003ewhere('age', '\u003e', 18)-\u003eget();`\n\n## 定义字段\n\n安全起见，过滤器使用字段白名单机制，每个过滤器都需要显式定义可查询的列、可关联的表、可用于排序的列。\n（如果没有定义，过滤器将不起任何作用）\n\n你需要显式实现如下方法：\n\n```php\n// App\\Filters\\UserFilter\n\n// 定义可用于条件查询的列名\nprotected function getFieldsQueryable(): array {\n    return [\n        'name',\n        'email',\n        'userinfo.age' // 关联查询\n    ]\n}\n\n// 定义可用于关联查询的表\nprotected function getReleasable(): array {\n    return [\n        'userinfo',\n        'addresses'\n    ]\n}\n\n// 定义可用于排序的列\nprotected function getSortable(): array {\n    return [\n        'id',\n        'created_at'\n    ]\n}\n```\n\n## 全局过滤\n\n有时候你可能想要使用全局的过滤，你可以重写 `applyBaseFilter` 方法\n\n这个方法应该返回 `Builder` 实例\n\n```php\n// App\\Filters\\UserFilter\n\nprotected function applyBaseFilter(Builder $builder): Builder\n{\n    return $builder-\u003ewhere('disabled', 0);\n}\n\n```\n\n## License\n\n[MIT](https://github.com/jiaxincui/laravel-query-filter/blob/master/LICENSE.md) © [JiaxinCui](https://github.com/jiaxincui)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjiaxincui%2Flaravel-request-query","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjiaxincui%2Flaravel-request-query","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjiaxincui%2Flaravel-request-query/lists"}