tecnocampos/dynamic-model-filter
最新稳定版本:v0.1.3
Composer 安装命令:
composer require tecnocampos/dynamic-model-filter
包简介
Apply request-based filters to Eloquent models in Laravel
README 文档
README
Apply dynamic filters to Eloquent models based on request data — including support for where, whereDate, between, like, and nested relation filters. Comes with a flexible Blade form component for easy integration.
🚀 Installation
1. Require the package (for local testing)
composer require tecnocampos/dynamic-model-filter
⚙️ Setup
The package auto-registers via ServiceProvider using Laravel's package discovery.
If you're loading views from the package, they will be available under the namespace:
dynamicfilters::
✅ Usage
1. Add the Trait to your Model
use TecnoCampos\DynamicModelFilter\Traits\FilterRequestScope; class User extends Model { use FilterRequestScope; public static array $filterRequest = [ 'status' => 'text', 'created_at' => 'date', 'search' => 'multi|type:like|fields:name,email', ]; }
2. Apply Filters in Controller
$users = User::applyFilters()->paginate();
🧠 Filter Syntax Reference
Each entry in public static array $filterRequest uses the format:
field_name => 'type|option1|option2|...'
Supported Types
| Type | Description |
|---|---|
text |
Basic where |
like |
where with %value% |
date |
whereDate |
between |
Two filters used for range (first, last) |
relation |
Uses whereHas with nested filters |
multi |
Search across multiple fields |
Extra Options
| Option | Usage |
|---|---|
first / last |
Used with between filters |
field:column |
When input name differs from DB field |
type:text/like |
Type used inside relation or multi-search |
relation:name |
Relation path (user.address) |
fields:field1,field2 |
Used with multi type |
source:request |
Fetch value from request() (default) |
🧩 Example: Advanced
public static array $filterRequest = [ 'start_date' => 'between|first|field:created_at', 'end_date' => 'between|last|field:created_at', 'status' => 'text', 'search' => 'multi|type:text|fields:name,email', 'subscription' => 'relation|type:text|field:id|relation:subscriptions', ];
🖼 Blade Component (Form) – Optional UI Integration
You can use the <x-DMF::form /> component to render filter forms automatically based on a simple fields array.
Basic usage
<x-DMF::form :fields="[ 'start_date' => ['type' => 'date', 'label' => __('Start Date')], 'end_date' => ['type' => 'date', 'label' => __('End Date')], 'status' => [ 'type' => 'select', 'label' => __('Status'), 'data' => ['active' => 'Active', 'inactive' => 'Inactive'], 'attributes' => '', 'class' => 'form-select' ], ]" />
- All submitted fields will persist values using request().
- The component will auto-render the correct field types (text, select, date).
UI Templates
You can define which template is used (Bootstrap or Tailwind) via config:
- Set in .env:
DMF_TEMPLATE=bootstrap
- Or override in config file config/dynamic-model-filter.php:
return [ 'template' => env('DMF_TEMPLATE', 'tailwind'), // or 'bootstrap' ];
The component will automatically load one of the following views:
- resources/views/components/bootstrap/filter-form.blade.php
- resources/views/components/tailwind/filter-form.blade.php
Dynamic behavior
- size: sets the grid column width (1–12) for Bootstrap and Tailwind layouts
- data: associative array for select
- attributes: any custom HTML attributes (multiple, readonly, etc.)
- class: additional CSS classes
📅 Date Support (pt_BR / en)
The filter handles date input in both BR (d/m/Y) and EN (m/d/Y) formats automatically using Carbon and app locale.
✅ Requirements
- PHP 8.0+
- Laravel 8+
🤝 Contributing
Contributions are welcome! If you find a bug or have an idea for an improvement, feel free to open an issue or submit a pull request.
How to Contribute
- Fork this repository
- Clone your fork:
git clone https://github.com/fredcampos3/dynamic-model-filter.git
- Create a new branch for your changes:
git checkout -b feature/your-feature-name
- Make your changes and commit:
git commit -m "Add feature: your description"
- Push to your fork:
git push origin feature/your-feature-name
- Open a Pull Request to the
mainbranch of this repository
Please ensure your code follows Laravel conventions and includes relevant tests if applicable.
📖 License
MIT © TecnoCampos
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-29