PHPackages                             jiaxincui/laravel-request-query - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jiaxincui/laravel-request-query

ActiveLibrary[Database &amp; ORM](/categories/database)

jiaxincui/laravel-request-query
===============================

eloquent request filter for laravel

0.1.7(3mo ago)03MITPHPPHP &gt;=8.1

Since Nov 24Pushed 3mo agoCompare

[ Source](https://github.com/jiaxincui/laravel-request-query)[ Packagist](https://packagist.org/packages/jiaxincui/laravel-request-query)[ RSS](/packages/jiaxincui-laravel-request-query/feed)WikiDiscussions main Synced today

READMEChangelog (5)Dependencies (10)Versions (7)Used By (0)

About
-----

[](#about)

Eloquent request filter for laravel

使用示例
----

[](#使用示例)

请求时使用对应的参数返回过滤后的数据

`/api/users?where=age:gt:18`

`/api/users?where=name:like:foo;email:like:bar`

`/api/users?where=age:gt:18&slice=5,10&orderBy=id,desc`

`/api/users?where[]=age:gt:18&where[]=name:foo&slice=5,10&orderBy=id,desc`

使用步骤
----

[](#使用步骤)

**1. 在 `Model` 里使用 `trait` 类**

`use Jiaxincui\RequestFilter\FilterScope;`

**2. 创建 `Filter` 类**

`php artisan make:filter User`

将生成一个 filter 类 `App\Filters\UserFilter`

你还需要显式定义可用于过滤的字段，见后续章节。

**3. 在查询时添加 `Filter` 类**

`User::fitler(new UserFilter)->get()`

**4. 查询请求**

`/api/users?where=age:gt:18;name:foo`

默认可用的方法
-------

[](#默认可用的方法)

### with

[](#with)

`with` 示例：`with=userinfo`

### orderBy

[](#orderby)

`orderBy` (大小写敏感)， 示例：`orderBy=id,desc`

### slice

[](#slice)

`slice` 示例： `slice=5,10`

### trashed

[](#trashed)

`trashed` 示例： `trashed=only`、`trashed=with`

### where

[](#where)

`where` 和 `where[]` 示例：`where=age:gt:18`、`where[]=name:like:foo&where[]=age:gt:18`

**操作符**

对于 `where` 一个查询参数分3段，并且使用`:` (冒号) 作为分隔符，分别代表字段、操作符、值

支持的操作符如下：

`eq` `lt` `gt` `lte` `gte` `neq` `in` `notin` `between` `notbetween` `null` `notnull`

**注意**

其中 `null` `notnull` 参数为2段

示例：`where=card_number:null`

**列表项**

列表项使用 `,` 作为分隔符

以下示例展示了分隔符 `,` 的使用

`api/users?where=name:in:foo,bar&with=userinfo,adress&orderBy=id,desc&slice=5,10`

自定义方法
-----

[](#自定义方法)

除了使用默认方法，你还可以在生成的类里自定义方法

```
// App\Filters\UserFilter

public function onlyVip($arg){
    $this->builder->where('vip', '=', $arg)
}
```

使用： `/api/users?onlyVip=1`

分组
--

[](#分组)

对于 `where` 查询需要区分 `and` 和 `or` , 以下示例可以告诉你答案

**请留意 `;` 符号的使用**

**示例1**

`/api/users?where=age:gt:18;name:foo`

SQL `age > 18 or neme = 'foo'`

**示例2**

`/api/users?where[]=age:gt:18&where[]=name:foo`

SQL `age > 18 and neme = 'foo'`

**示例3**

`/api/users?where[]=age:lte:18;vip:eq:1&where[]=name:foo`

SQL `(age > 18 or vip = 1) and neme = 'foo'`

关联查询
----

[](#关联查询)

基本

`/api/users?with=userinfo` 等同于 `User::with('userinfo')->get();`

带条件的关联查询

`/api/users?with=userinfo&where=userinfo.age:gt:18` 等同于 `User::whereHas('userinfo', fn ($query) => $query->where('age', '>', 18)->get();`

定义字段
----

[](#定义字段)

安全起见，过滤器使用字段白名单机制，每个过滤器都需要显式定义可查询的列、可关联的表、可用于排序的列。 （如果没有定义，过滤器将不起任何作用）

你需要显式实现如下方法：

```
// App\Filters\UserFilter

// 定义可用于条件查询的列名
protected function getFieldsQueryable(): array {
    return [
        'name',
        'email',
        'userinfo.age' // 关联查询
    ]
}

// 定义可用于关联查询的表
protected function getReleasable(): array {
    return [
        'userinfo',
        'addresses'
    ]
}

// 定义可用于排序的列
protected function getSortable(): array {
    return [
        'id',
        'created_at'
    ]
}
```

全局过滤
----

[](#全局过滤)

有时候你可能想要使用全局的过滤，你可以重写 `applyBaseFilter` 方法

这个方法应该返回 `Builder` 实例

```
// App\Filters\UserFilter

protected function applyBaseFilter(Builder $builder): Builder
{
    return $builder->where('disabled', 0);
}
```

License
-------

[](#license)

[MIT](https://github.com/jiaxincui/laravel-query-filter/blob/master/LICENSE.md) © [JiaxinCui](https://github.com/jiaxincui)

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance82

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~32 days

Total

5

Last Release

91d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/24616789?v=4)[Jiaxin.cui](/maintainers/jiaxincui)[@jiaxincui](https://github.com/jiaxincui)

---

Top Contributors

[![jiaxincui](https://avatars.githubusercontent.com/u/24616789?v=4)](https://github.com/jiaxincui "jiaxincui (10 commits)")

---

Tags

eloquentfilterlaravelqueryrequest

### Embed Badge

![Health badge](/badges/jiaxincui-laravel-request-query/health.svg)

```
[![Health](https://phpackages.com/badges/jiaxincui-laravel-request-query/health.svg)](https://phpackages.com/packages/jiaxincui-laravel-request-query)
```

###  Alternatives

[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M195](/packages/laravel-ai)[illuminate/queue

The Illuminate Queue package.

21332.6M1.6k](/packages/illuminate-queue)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9762.4M131](/packages/roots-acorn)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45444.2k1](/packages/pressbooks-pressbooks)[moonshine/moonshine

Laravel administration panel

1.3k253.1k81](/packages/moonshine-moonshine)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
