PHPackages                             thepatter/query-common - 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. thepatter/query-common

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

thepatter/query-common
======================

a query common of laravel

27PHP

Since Apr 8Pushed 6y ago1 watchersCompare

[ Source](https://github.com/Thepatterraining/queryCommon)[ Packagist](https://packagist.org/packages/thepatter/query-common)[ RSS](/packages/thepatter-query-common/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

queryCommon
===========

[](#querycommon)

查询公用框架，服务于laravel框架

可快速轻松完成查询列表，查询一条记录

创建一个`controller`，继承`QueryController`, 实现下面的函数即可返回要查询的列表

```
/*
     * 查询列表
     * @route get.api/lists
     */
    public function getList(){
        try{
            //检查页码，搜索条件等
            $this->pageValid();

            //返回数据
            $data = $this->pageList();
            return $this->success($data);
        } catch (Exception $ex) {

        }

    }
```

通过重写下面的函数来决定主要的`model`

```
protected function getModel() {
    return new Model;
}
```

为什么是主要的model呢，因为如果需要连表查询，使用left join的话，会以上面的Model为主表，那么怎么使用呢，看下面的例子：

首先需要在你的类里实现`WithJoins`接口

然后重写`getJoins`函数，该函数返回一个二维数组，可以连接多张表进行查询

```
class Controller extends QueryController implements WithJoins
{
    function getJoins(): array
    {
        return [['表名','主表.字段','=','表名.字段'],...];
    }
}
```

默认查询所有字段，如果需要自定义字段也很简单

首先需要在你的类里实现`WithFields`接口

然后重写`getFields`函数，该函数返回一个字段数组，如果连表查询，需要写成`表名.字段名`的形式

```
class Controller extends QueryController implements WithFields
{
    function getFields(): array
    {
        return ['字段名','表名.字段名',...];
    }
}
```

这里的列表默认按照时间字段created\_at倒序排序，如果需要自定义排序也很简单

首先在你的类里实现`WithOrderBy`接口

然后重写`getOrderBy`函数，返回一个数组，支持多字段排序

```
class Controller extends QueryController implements WithOrderBy
{
    function getOrderBy(): array
    {
        return ['要排序的字段名' => 'asc|desc',...];
    }
}
```

常见的还有需要搜索查询功能，默认查询是下面这样的：

比如要查询用户名是张三的用户，前端可以传过来

\['userName' =&gt; '张三'\]

当然这是精确匹配，如果要模糊查询怎么办呢，前端只需要传过来这样的：

\['userName' =&gt; \['like', '张三'\]\]

这样前端有什么查询需求，后端都不需要改代码了。

如果想要自定义查询功能，也很简单

首先在你的类里实现`WithWheres`接口

然后重写`getWheres`函数，返回一个数组，数组内容就是laravel where里面使用的数组信息

```
class Controller extends QueryController implements WithWheres
{
    function getWheres(): array
    {
        return ['字段名' => '查询内容'];
    }
}
```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity34

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/20748744?v=4)[Thepatterraining](/maintainers/Thepatterraining)[@Thepatterraining](https://github.com/Thepatterraining)

---

Top Contributors

[![Thepatterraining](https://avatars.githubusercontent.com/u/20748744?v=4)](https://github.com/Thepatterraining "Thepatterraining (11 commits)")

### Embed Badge

![Health badge](/badges/thepatter-query-common/health.svg)

```
[![Health](https://phpackages.com/badges/thepatter-query-common/health.svg)](https://phpackages.com/packages/thepatter-query-common)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M117](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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