ahmmmmad11/filters 问题修复 & 功能扩展

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

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

ahmmmmad11/filters

Composer 安装命令:

composer require ahmmmmad11/filters

包简介

A wrapper package over spatie query builder package to simplify filters creation for Laravel Apps

README 文档

README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

Installation

You can install the package via composer:

composer require ahmmmmad11/filters

After installation, you can publish the configuration file using the following command:

php artisan vendor:publish --tag="filters-config"

This is the contents of the published config file:

return [
    /*
    |--------------------------------------------------------------------------
    | Filters Path
    |--------------------------------------------------------------------------
    |
    | This value is the path where your filter class will be created.
    |
    */

    'path' => '\Http\Filters',

    /*
    |--------------------------------------------------------------------------
    | pagination rows
    |--------------------------------------------------------------------------
    |
    | The `per_page` value indicates the default pagination size. 
    | If the 'per_page' argument is not provided or there is no 
    | paginate query in the request, this value will be used.
    |
    */

    'per_page' => 100,
];

Usage

create your first filter

php artisan filter:make UsersFilter --model=User

This will generate a UsersFilter class in the ‘app/Http/Filters’ directory. You can then customize this filter according to your application’s needs.

    <?php

    namespace App\Http\Filters;
    
    use Ahmmmmad11\Filters\Filter;
    use App\Models\User;
    use Spatie\QueryBuilder\QueryBuilder;
    
    class UsersFilter extends Filter
    {
        public function filter(): Filter
        {
            $this->data = QueryBuilder::for(User::class)
                ->allowedFilters(
                    ["id","name","email","email_verified_at","created_at","updated_at"]
                );
    
            return $this;
        }
    }

Certainly! Here’s a rephrased version of your instructions:

If you follow the correct naming convention, you can omit the --model option. For instance, if you have a User model, you can use the following simplified command:

    php artisan filter:make UsersFilter

In this case, since you used the plural form of the model (User becomes “Users”), or if you prefer the singular form (UserFilter), the filter command will automatically associate it with the User model.

Please note that this rule does not apply to combined model names like UserProduct. In such cases, please explicitly specify the model using the --model=UserProduct option or the shorter -m"UserProduct" form.

now you can use the filter by injecting UserFilter in your controller like:

...

use App\Http\Filters\UsersFilter;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(UsersFilter $filter)
    {
        return $filter->get();
    }
}

or you may want to return paginated data

    ...
    
    public function index(UsersFilter $filter)
    {
        return $filter->paginate(30);
    }

if you want to assign the size of the pagination form the client side you can do it by leaving the paginate argument empty

    $filter->paginate();

    // https://example.com/users?per_page=10

if the rows argument of paginate method is left empty and no ?paginate in request query the default row size in filters.php config will be used.

Extend Eloquent methods

you can preform customization over the query directly from you controller method by passing callback to execute method.

// UsersController

public function index(UsersFilter $filter)
{
    return $filter->execute(function ($query) {
        $query->where('status', 'active');
    })->get();
}

inside execute callback function you can use all eloquent methods.

or you can directly chain eloquent methods

public function index(UsersFilter $filter)
{
    return $filter->where('status', 'active')->get();
}

Include relations

to include model relations just add option --relations to filter make command.

php artisan filter:make UsersFilter  --relations

or short form

php artisan filter:make UsersFilter -r

this will generate:

//UserFilter Class

public function filter(): Filter
    {
        $this->data = QueryBuilder::for(User::class)
            ->allowedFilters(
                [...filters]
            )
            ->allowedIncludes(
                [...relations]
            );

        return $this;
    }

for more details check Spatie Laravel-query-builder

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

if you fond any security vulnerability send a direct email to me alamerahmed00@gmail.com

License

The MIT License (MIT). Please see License File for more information.

ahmmmmad11/filters 适用场景与选型建议

ahmmmmad11/filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.11k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 03 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 ahmmmmad11/filters 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-07