定制 larajs/query 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

larajs/query

最新稳定版本:2.0.1

Composer 安装命令:

composer require larajs/query

包简介

README 文档

README

outline deep
title LaraJS Query - Dynamic API Query Builder for Laravel
description LaraJS Query simplifies Eloquent models filtering, sorting, and including relationships with a flexible interface for client-side querying in Laravel applications
author LaraJS Team
head
meta
name content
keywords LaraJS Query, Laravel query builder, Laravel filtering, API query builder, Laravel sorting, Laravel relationships, Eloquent query builder, Laravel pagination, dynamic filtering, Laravel API, Eloquent models, Laravel repository pattern
meta
name content
robots index, follow
meta
name content
twitter:card summary_large_image
meta
name content
twitter:title LaraJS Query - Dynamic API Query Builder for Laravel
meta
name content
twitter:description Build powerful and flexible Laravel Eloquent queries with LaraJS Query for dynamic filtering, sorting, and relationship handling
meta
name content
twitter:image https://docs.larajs.com/larajs.png
meta
property content
og:title LaraJS Query - Dynamic API Query Builder for Laravel
meta
property content
og:description Build powerful and flexible Laravel Eloquent queries with LaraJS Query for dynamic filtering, sorting, and relationship handling
meta
property content
og:url https://docs.larajs.com/packages/larajs-query.html
meta
property content
og:image https://docs.larajs.com/larajs.png
meta
property content
og:type article
link
rel href
canonical https://docs.larajs.com/packages/larajs-query.html

LaraJS Query

Dynamic HTTP query parameter builder for Laravel Eloquent. Filter, sort, search, include relationships, select fields, and paginate using a functional query syntax.

Installation

composer require larajs/query:^2.0

Requirements: PHP 8.3+, Laravel 11/12/13

Quick Start

use App\Models\User;
use LaraJS\Query\LaraJSQuery;
use LaraJS\Query\DTO\{QueryParserAllowDTO, QueryParserRequestDTO};

class UserController
{
    use LaraJSQuery;

    public function index(Request $request)
    {
        return User::query()
            ->applyLaraJSQuery(
                QueryParserRequestDTO::fromArray($request->query()),
                QueryParserAllowDTO::fromArray([
                    'filter' => ['name', 'email'],
                    'include' => ['posts', 'roles'],
                    'sort' => ['name', 'created_at'],
                ])
            )
            ->get();
    }
}

Usage:

GET /api/users?filter=equals(name,'John')&sort=name&include[]=posts&pagination[limit]=10

Filtering

Filter with functional IBM-style syntax. All filters support relationship variants (e.g., equalsRelation, greaterThanRelation).

Function Example
equals ?filter=equals(name,'Smith')
greaterThan / lessThan ?filter=greaterThan(age,'25')
greaterOrEqual / lessOrEqual ?filter=greaterOrEqual(price,'100')
contains / startsWith / endsWith ?filter=contains(title,'Laravel')
any ?filter=any(status,'active','pending')
between ?filter=between(created_at,'2025-01-01','2025-12-31')
has ?filter=has(posts,'1')
relation ?filter=relation(author,equals(country,'US'))
and / or / not ?filter=and(equals(role,'admin'),greaterThan(age,'25'))

Sorting

Sort by single/multiple columns, including relationships via BelongsToThrough.

Type Example
Ascending ?sort=name
Descending ?sort=-name
Multiple ?sort=name,-created_at
Relationship ?sort=author.name
Relationship Count ?sort=posts_count

Searching

LIKE-based search across columns and relationships.

Type Example
Single column ?search[column]=name&search[value]=john
Multiple columns ?search[column]=name,email&search[value]=john
Relationship ?search[column]=author.name&search[value]=smith

Including Relationships

Eager load relationships with nested support, aggregates, and filtering.

Type Example
Single ?include[]=posts
Multiple ?include[]=posts&include[]=roles
Nested ?include[]=posts.comments
Aggregates ?include[]=posts|count&include[]=roles|exists
Filtered ?include[]=posts|and(equals(status,'published'))

Supported aggregates: count, exists, sum, min, max, avg

Selecting Fields

Project specific columns via select parameter.

?select=id,name,email

Date Filtering

Filter by date ranges. Auto-calculates startOfDay/endOfDay boundaries.

Type Example
Array format ?date[column]=created_at&date[value][0]=2025-01-01&date[value][1]=2025-12-31
Filter syntax ?filter=between(created_at,'2025-01-01','2025-12-31')

Pagination

Three pagination types with configurable limits (default 25, max 500).

Type Example
Default ?pagination[limit]=25&pagination[page]=1
Simple ?pagination[type]=simple&pagination[limit]=25&pagination[page]=1
Cursor ?pagination[type]=cursor&pagination[cursor]=...

Security: Allow-List Whitelisting

Control queryable fields for each endpoint. By default, nothing is exposed — explicitly whitelist what clients can query.

$allow = QueryParserAllowDTO::fromArray([
    ‘field’   => [‘id’, ‘name’, ‘email’],         // Projectable columns
    ‘filter’  => [‘name’, ‘email’, ‘status’],     // Filterable fields
    ‘sort’    => [‘name’, ‘created_at’],          // Sortable columns
    ‘include’ => [‘posts’, ‘roles’],              // Includable relations
    ‘search’  => [‘name’, ‘email’],               // Searchable fields
    ‘date’    => [‘created_at’],                  // Date-filterable fields
]);

Repository Pattern

Use repositories to separate business logic from HTTP concerns.

// Repository
class UserRepository extends ReadRepository
{
    public function __construct()
    {
        parent::__construct(new User(), 25, 500);
    }
}

// Controller
class UserController
{
    public function __construct(private UserRepository $users) {}

    public function index(Request $request)
    {
        return $this->users->findAll(
            QueryParserAllowDTO::fromArray([...])
        );
    }
}

See /docs/deployment-guide.md for complete repository patterns.

Documentation

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2026-05-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固