{"id":26232746,"url":"https://github.com/kalimeromk/filterable","last_synced_at":"2025-04-22T10:42:10.872Z","repository":{"id":37471578,"uuid":"483820498","full_name":"KalimeroMK/filterable","owner":"KalimeroMK","description":"In Laravel, we commonly face the problem of adding repetitive filtering code, this package will address this problem.","archived":false,"fork":false,"pushed_at":"2025-01-27T14:22:09.000Z","size":156,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T14:21:20.882Z","etag":null,"topics":["filtering","filters","laravel","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KalimeroMK.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-20T21:36:39.000Z","updated_at":"2025-01-27T14:22:12.000Z","dependencies_parsed_at":"2023-11-27T20:45:08.445Z","dependency_job_id":null,"html_url":"https://github.com/KalimeroMK/filterable","commit_stats":{"total_commits":25,"total_committers":3,"mean_commits":8.333333333333334,"dds":"0.16000000000000003","last_synced_commit":"ee677b978c81a67a45335408d092eabe74abbc21"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KalimeroMK%2Ffilterable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KalimeroMK%2Ffilterable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KalimeroMK%2Ffilterable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KalimeroMK%2Ffilterable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KalimeroMK","download_url":"https://codeload.github.com/KalimeroMK/filterable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250222208,"owners_count":21394835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filtering","filters","laravel","php"],"created_at":"2025-03-13T00:38:26.054Z","updated_at":"2025-04-22T10:42:10.793Z","avatar_url":"https://github.com/KalimeroMK.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Filterable - Laravel Package for Dynamic Filtering\n\n**Filterable** is a Laravel package designed to simplify dynamic filtering and searching across models and their relationships, eliminating the need for repetitive query code. It provides an easy-to-use trait and macros for building powerful, dynamic query filters.\n\n---\n\n## Installation\n\n1. Require the package via Composer:\n   ```bash\n   composer require kalimeromk/filterable\n   ```\n\n2. The package supports auto-discovery, so no manual registration of the service provider is needed. However, if you are using an older Laravel version, add the service provider to your `config/app.php`:\n   ```php\n   'providers' =\u003e [\n       Kalimeromk\\Filterable\\PackageServiceProvider::class,\n   ];\n   ```\n\n---\n\n## Usage\n\n### 1. Using `whereLike` Macro\n\nThe `whereLike` macro allows you to perform \"LIKE\" searches on multiple fields, including related model fields.\n\n#### Example:\n\n```php\nuse App\\Models\\User;\n\n$users = User::query()\n    -\u003ewhereLike(['name', 'email'], 'John')\n    -\u003eget();\n```\n\nThis will return all users where the `name` or `email` fields contain the string \"John\".\n\n#### Searching on Related Models:\n\n```php\n$users = User::query()\n    -\u003ewhereLike(['posts.title', 'posts.content'], 'Laravel')\n    -\u003ewith('posts')\n    -\u003eget();\n```\n\nThis will return all users who have posts with a title or content containing the string \"Laravel\".\n\n---\n\n### 2. Using the `Filterable` Trait\n\nThe `Filterable` trait allows you to dynamically filter a model based on specific criteria, including `LIKE` queries, boolean fields, and `whereIn` filters, as well as range filters like `_min` and `_max`.\n\n#### Setup:\nAdd the `Filterable` trait to your model:\n\n```php\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Kalimeromk\\Filterable\\Traits\\Filterable;\n\nclass User extends Model\n{\n    use Filterable;\n\n    protected $fillable = ['name', 'email', 'is_active'];\n\n    // Define fields for specific filters\n    protected $boolFields = ['is_active'];\n    protected $likeFields = ['name', 'email'];\n}\n```\n\n#### Example Controller:\n\n```php\nuse App\\Models\\User;\nuse Illuminate\\Http\\Request;\n\nclass UserController extends Controller\n{\n    public function index(Request $request)\n    {\n        $filters = $request-\u003eonly(['name', 'email', 'is_active', 'age_min', 'age_max']);\n\n        $users = User::filter($filters)-\u003eget();\n\n        return response()-\u003ejson($users);\n    }\n}\n```\n\n#### Example API Request:\n- **Request:**\n  ```\n  GET /users?name=Jane\u0026is_active=true\u0026age_min=25\u0026age_max=35\n  ```\n- **Result:**\n  Returns all active users (`is_active = true`) whose name contains \"Jane\" and whose age is between 25 and 35.\n\n---\n\n## Testing the Package\n\nThis package uses [Orchestra Testbench](https://github.com/orchestral/testbench) for testing.\n\n### Running Tests\n\nTo run the tests:\n\n1. Install the dependencies:\n   ```bash\n   composer install\n   ```\n\n2. Run PHPUnit:\n   ```bash\n   vendor/bin/phpunit\n   ```\n\n---\n\n## License\n\nThis package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkalimeromk%2Ffilterable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkalimeromk%2Ffilterable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkalimeromk%2Ffilterable/lists"}