定制 goodcat/laravel-filter-querystring 二次开发

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

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

goodcat/laravel-filter-querystring

Composer 安装命令:

composer require goodcat/laravel-filter-querystring

包简介

A laravel package to filter Eloquent models using query string parameters

README 文档

README

Latest Version on Packagist GitHub Tests Action Status

This package allows you to filter Eloquent models using query string parameters.

Quick Start

Get started with laravel-filter-querystring in three steps.

  1. Download the package via Composer.

    composer require goodcat/laravel-filter-querystring
  2. Add the UseQueryString trait to your model and tag a method with the QueryString attribute.

    use Illuminate\Database\Eloquent\Builder;
    use Goodcat\QueryString\Traits\UseQueryString;
    use Goodcat\QueryString\Attributes\QueryString;
    
    class User extends Authenticatable
    {
        use UseQueryString;
        
        #[QueryString('email')]
        public function filterByEmail(Builder $query, string $search): void
        {
            $query->where('email', $search);
        }
    }
  3. Use the queryString() scope when you want to filter models based on query string parameters in the request.

    class UserController extends Controller
    {
        public function index(Request $request): View
        {
            // E.g. https://example.com/users?email=john@doe.com
            $users = User::query()->queryString($request)->get();
    
            return view('user.index', ['users' => $users]);
        }
    }

That's it. You're all set to start using laravel-filter-querystring.

Caching

laravel-filter-querystring scans your code to locate methods tagged with the QueryString attribute. While the performance impact of this discovery is negligible, you can cache these methods using the Artisan command querystring:cache. To clear the cache, use the Artisan command querystring:clear.

php artisan querystring:cache

To streamline your deployment process, laravel-filter-querystring integrates with Laravel's optimize command. Use the optimize and optimize:clear commands to create and remove the cache.

Configuration

To publish the config file to config/querystring.php run the command:

php artisan vendor:publish --provider="Goodcat\QueryString\QueryStringServiceProvider"

Handling null values

The null values are ignored by laravel-filter-querystring. If you want null values passed to your function, set 'allows_null' to true in config/querystring.php file.

Digging deeper

Let's take a closer look at how laravel-filter-querystring works under the hood and explore its advanced features.

#[QueryString] attribute

The QueryString attribute is used to map the name of a query string to a method. The attribute name must match the query string name.

#[QueryString('name')]
public function filterByName(Builder $query, string $search): void 

E.g. The string name in the URL http://example.com/?name=John+Doe is mapped to the method tagged with the #[QueryString('name')] attribute.

Filter methods

The filter method receives three parameters: the query builder, the query string value and the query string name. You can add multiple attributes to the same method.

#[QueryString('name', 'email')]
#[QueryString('description')]
public function genericStringSearch(Build $query, string $search, string $name): void
{
    $query->where($name, 'like', "$search%");
}

queryString() scope

The queryString() scope is responsible for calling your filter methods. It accepts a Request or an array<string, string>.

public function index(Request $request): View
{
    $filters = $request->query();
    
    // Change $filters array as desired.
    
    $users = User::query()->queryString($filters)->get();
    
    return view('user.index', ['users' => $users]);
}

Laravel uses TrimStrings and ConvertEmptyStringsToNull middlewares to trim and nullify empty strings from requests. If you pass an array to the filter method, it's up to you to normalize the passed value.

Filter object

By default, laravel-filter-querystring searches the model for filter methods. If you wish, you can register a different class by overriding the getQueryStringObject() method.

   class User extends Authenticatable
   {
       use UseQueryString;

        protected function getQueryStringObject(): object
        {
            return new CustomFilterClass();
        }
   }

goodcat/laravel-filter-querystring 适用场景与选型建议

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

它主要适用于以下技术方向: 「model」 「laravel」 「filters」 「eloquent」 「query-string」 「goodcat」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 goodcat/laravel-filter-querystring 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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