behindsolution/laravel-query-gate 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

behindsolution/laravel-query-gate

Composer 安装命令:

composer require behindsolution/laravel-query-gate

包简介

Generic HTTP gateway for Laravel Eloquent queries.

README 文档

README

Latest Version Tests Total Downloads License

A declarative API layer for Laravel that turns Eloquent models into fully featured REST endpoints — with filtering, sorting, actions, versioning, OpenAPI docs, and a type-safe TypeScript SDK.

Features

  • Declarative API definition — define filters, sorts, selects, and actions directly on your model
  • CRUD actions — built-in create, update, delete, and detail with validation, policies, and custom handlers
  • Custom actions — extend your API with reusable action classes
  • API versioning — version your endpoints with automatic changelog generation
  • OpenAPI documentation — auto-generated spec and UI out of the box
  • TypeScript code generation — generate type-safe contracts with php artisan qg:types
  • Frontend SDK — fully typed, framework-agnostic query builder for the frontend
  • Caching & pagination — cursor and offset pagination, built-in cache support

Requirements

  • PHP 8.2+
  • Laravel 10, 11, or 12

Installation

composer require behindsolution/laravel-query-gate

Publish the config file:

php artisan vendor:publish --tag=query-gate-config

Quick Start

Add the trait to your model and define the query gate:

use BehindSolution\LaravelQueryGate\Traits\HasQueryGate;
use BehindSolution\LaravelQueryGate\Support\QueryGate;

class User extends Model
{
    use HasQueryGate;

    public static function queryGate(): QueryGate
    {
        return QueryGate::make()
            ->alias('users')
            ->select(['id', 'name', 'email', 'created_at'])
            ->filters(['name' => 'string', 'email' => 'email'])
            ->allowedFilters(['name' => ['like', 'eq'], 'email' => ['eq']])
            ->sorts(['name', 'created_at']);
    }
}

Your API is ready:

GET    /query/users?filter[name][like]=John&sort=-created_at
GET    /query/users/1

Actions

Define mutations with a fluent builder:

QueryGate::make()
    ->alias('posts')
    ->actions(fn ($actions) => $actions
        ->create(fn ($action) => $action
            ->validations(['title' => 'required|string', 'body' => 'required|string'])
            ->policy('create')
        )
        ->update(fn ($action) => $action
            ->validations(['title' => 'string', 'body' => 'string'])
            ->policy('update')
        )
        ->delete(fn ($action) => $action->policy('delete'))
        ->detail()
    );

Custom actions:

->actions(fn ($actions) => $actions
    ->use(PublishPostAction::class)
)

API Versioning

QueryGate::make()
    ->alias('users')
    ->version('2024-01-01', fn ($gate) => $gate
        ->select(['id', 'name', 'email'])
        ->filters(['name' => 'string'])
    )
    ->version('2024-06-01', fn ($gate) => $gate
        ->select(['id', 'name', 'email', 'avatar'])
        ->filters(['name' => 'string', 'email' => 'email'])
    );

Clients select a version via the X-Query-Version header or ?version= query parameter. A changelog is auto-generated at GET /query/users/__changelog.

TypeScript Code Generation

Generate type-safe contracts from your API definitions:

php artisan qg:types --output=resources/ts/contracts

Output example:

export interface UserEntity {
  id: number;
  name: string;
  email: string;
}

export interface UserCreatePayload {
  name: string;
  email: string;
}

export interface UserResourceContract {
  get: UserEntity;
  create: { payload: UserCreatePayload; response: UserEntity };
  // ...
}

Frontend SDK

Install the companion SDK:

npm install laravel-query-gate-sdk
import { configureQueryGate, queryGate } from 'laravel-query-gate-sdk'

configureQueryGate({ baseUrl: 'https://api.example.com/query' })

// List with filters and sorting
const users = await queryGate<UserResourceContract>('users')
  .filter('name', 'like', 'John')
  .sort('created_at', 'desc')
  .get()

// Create
await queryGate<UserResourceContract>('users')
  .post({ name: 'Jane', email: 'jane@example.com' })

// Update
await queryGate<UserResourceContract>('users').id(1)
  .patch({ name: 'Jane Doe' })

// Custom action
await queryGate<PostResourceContract>('posts').id(1)
  .action('publish').post()

OpenAPI Documentation

Enable in config/query-gate.php:

'openAPI' => [
    'enabled' => true,
],

Access the generated docs at GET /query/docs (UI) or GET /query/docs.json (spec).

Ecosystem

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

License

MIT

behindsolution/laravel-query-gate 适用场景与选型建议

behindsolution/laravel-query-gate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60 次下载、GitHub Stars 达 50, 最近一次更新时间为 2025 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 behindsolution/laravel-query-gate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 behindsolution/laravel-query-gate 我们能提供哪些服务?
定制开发 / 二次开发

基于 behindsolution/laravel-query-gate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 60
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 50
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 50
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-27