lacodix / laravel-model-filter
一个Laravel包,轻松实现从数据库中获取模型时的过滤、搜索和排序。
v3.0.3
2024-09-15 15:15 UTC
Requires
- php: ^8.1
- ext-intl: *
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- illuminate/database: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- laravel/pint: ^1.0
- laravel/prompts: ^0.1.16
- nunomaduro/collision: ^7.0|^8.0
- nunomaduro/larastan: ^2.0.1
- nunomaduro/phpinsights: ^2.6
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.21
- pestphp/pest-plugin-faker: ^2.0
- pestphp/pest-plugin-laravel: ^2.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^10.0
- rector/rector: ^1.0
- spatie/pest-plugin-test-time: ^2.0
- dev-master
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v1.x-dev
- v1.11.3
- v1.11.2
- v1.11.1
- v1.11.0
- v1.10.0
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.5
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.0
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- v0.8.1
- v0.8.0
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- dev-feat/nested-search
- dev-style/stan
- dev-docs/update-docs
- dev-renky-patch-1
- dev-fix/belongs-to-filter
- dev-doc-v3-fix
- dev-v3-doc-update
This package is auto-updated.
Last update: 2024-09-25 13:13:12 UTC
README
此包允许您轻松地从数据库中过滤、搜索和排序模型。它包含使用查询字符串进行过滤、搜索和排序的附加功能。
安装完成后,您可以过滤、搜索和排序模型。您可以根据我们的基础过滤器创建自己的过滤器,创建单个过滤器,或者使用扩展过滤器,如TrashedFilter,这些过滤器无需或只需少量配置即可使用。
此外,您还可以使用过滤器的可视化功能。
文档
您可以在我们的文档网站上找到此包的完整文档。
安装
composer require lacodix/laravel-model-filter
基本用法
过滤
创建第一个过滤器
php artisan make:filter CreatedAfterFilter --type=date --field=created_at
// Set the filter mode // App\Models\Filters\CreatedAfterFilter public FilterMode $mode = FilterMode::GREATER_OR_EQUAL; // Apply this filter and the HasFilters trait to a Model // App\Models\Post use HasFilters; protected array $filters = [ CreatedAfterFilter::class, ]; // Somwhere in a controller, select all posts created after 1st of January 2023 Post::filter(['created_after_filter' => '2023-01-01'])->get(); // Do the same via query string by calling // this url: https://.../posts?created_after_filter=2023-01-01 Post::filterByQueryString()->get();
搜索
// add searchable fields and the IsSearchable trait to Model: // App\Models\Post use IsSearchable; protected array $searchable = [ 'title', 'content', ]; // Somewhere in controller, find all posts that contain "test" in title or content Post::search('test')->get(); // Do the same via query string by calling // this url: https://.../posts?search=test Post::searchByQueryString()->get();
可视化
所有过滤器都有一个blade模板,可以可视化一个或多个输入字段。要可视化特定模型的全部过滤器,可以使用blade组件。
<x-lacodix-filter::model-filters :model="Post::class" />
分组
有时您不需要在Web应用程序的所有部分中使用所有过滤器。可能后端应该提供与前端不同的过滤器,或者不同类型的用户应该能够使用不同的过滤器。
在这种情况下,此包在向模型添加过滤器时提供了过滤器分组功能。
protected array $filters = [ 'frontend' => [ HotFilter::class, ], 'backend' => [ CreatedAfterFilter::class, PublishedFilter::class, ] ];
组可以在作用域中使用
Post::filterByQueryString('frontend')->get()
或
Post::filter(['hot_filter' => 'hot'], 'frontend')->get(); Post::filter(['created_after_filter' => '2023-01-01'], 'backend')->get();
测试
composer test
贡献
请在提交之前运行以下命令并解决潜在问题,并考虑为新功能添加测试。
composer rector:test composer insights composer csfixer:test composer phpstan:test
变更日志
有关最近更改的更多信息,请参阅变更日志。
致谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。