{"id":34013810,"url":"https://github.com/mjedari/larafilter","last_synced_at":"2025-12-13T13:41:57.266Z","repository":{"id":55595125,"uuid":"322013308","full_name":"mjedari/larafilter","owner":"mjedari","description":"This package is a simple object-oriented query string filter for Laravel applications. you can create a filter class -by command and make your query filtering logic clean and strongly maintainable.","archived":false,"fork":false,"pushed_at":"2021-12-27T17:23:23.000Z","size":195,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-30T02:02:39.814Z","etag":null,"topics":["filter","laravel","query-string"],"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/mjedari.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2020-12-16T14:50:59.000Z","updated_at":"2024-06-23T12:36:19.000Z","dependencies_parsed_at":"2022-08-15T04:00:32.998Z","dependency_job_id":null,"html_url":"https://github.com/mjedari/larafilter","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/mjedari/larafilter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjedari%2Flarafilter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjedari%2Flarafilter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjedari%2Flarafilter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjedari%2Flarafilter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mjedari","download_url":"https://codeload.github.com/mjedari/larafilter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjedari%2Flarafilter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27706472,"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","status":"online","status_checked_at":"2025-12-13T02:00:09.769Z","response_time":147,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["filter","laravel","query-string"],"created_at":"2025-12-13T13:41:55.918Z","updated_at":"2025-12-13T13:41:57.253Z","avatar_url":"https://github.com/mjedari.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](https://raw.githubusercontent.com/mjedari/larafilter/master/art/banner.png)\n\n# Laravel Query String Filter\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/mjedari/larafilter.svg?style=flat-square)](https://packagist.org/packages/mjedari/larafilter)\n[![Build Status](https://scrutinizer-ci.com/g/mjedari/larafilter/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mjedari/larafilter/build-status/master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mjedari/larafilter/badges/quality-score.png?b=master\u0026style=flat-square)](https://scrutinizer-ci.com/g/mjedari/larafilter/?branch=master)\n[![Quality Score](https://img.shields.io/scrutinizer/g/mjedari/larafilter.svg?style=flat-square)](https://scrutinizer-ci.com/g/mjedari/larafilter)\n![PHP Tests](https://github.com/mjedari/larafilter/workflows/PHP%20Tests/badge.svg)\n![PHP 8 Tests](https://github.com/mjedari/larafilter/workflows/PHP%208%20Tests/badge.svg?style=flat)\n[![Total Downloads](https://img.shields.io/packagist/dt/mjedari/larafilter.svg?style=flat-square)](https://packagist.org/packages/mjedari/larafilter)\n\n\nThis is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.\n\n# Quick start\n### Create a filter\n```bash\nphp artisan make:filter country\n```\n\n### Implement your filter logic\nIn the filter class which is created, implement your login in the ``apply()`` method.\nIn order to get query value just use ``$this-\u003evalue``. We retrieved it for you from your request.\n```php\n...\n\npublic function apply(Builder $builder)\n{\n    return $builder-\u003ewhere('country', $this-\u003evalue);\n}\n\n...\n```\n\n### Register filter class for model\nBefore registering you should use ``Filterable`` trait in your model.\n```php\nuse Filterable;\n...\n\n...\n\nprotected static $filters = [\n    Country::class,\n];\n\n```\n### Use it!\n\n```php\n// All registered filtered are available through this method:\n\nUser::filter()-\u003eget();\n\n\n// Only Specific registered filter is available and executable:\n\nUser::filterThrough([Country::class])-\u003eget();\n\n```\n***\n# Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require mjedari/larafilter\n```\nThen you can publish config file:\n```bash\nphp artisan vendor:publish --provider \"Mjedari\\Larafilter\\LarafilterServiceProvider\"\n```\n# Usage\n\n## Initiation\nIts simple.First create a filter by this command:\n```bash\nphp artisan make:filter filter-name\"\n```\nCommand will create a class under the default directory ``App\\Filters`` : \n```php\nnamespace App\\Filters;\n\nuse Illuminate\\Database\\Eloquent\\Builder;\nuse Mjedari\\Larafilter\\Filters\\FilterContract;\n\nclass Active extends FilterContract\n{\n\n   public function apply(Builder $builder)\n   {\n       // TODO: Implement apply() method.\n   }\n\n   public function options()\n   {\n       // TODO: Implement options() method.\n   }\n\n   /*\n   * Set rules on the query string\n   */\n   public function rules()\n   {\n       return [\n           //\n       ];\n   }\n\n}\n```\nYour filter logic would be implemented in the ``apply()`` method:\n```php\npublic function apply(Builder $builder)\n{\n    return $builder-\u003ewhere('avtive', $this-\u003evalue);\n}\n```\nThe important thing is that you have access query string value by ``$this-\u003evalue`` in your filter class.\n\n## Using\n\nFor Which model you want to filter you should add ``Filterable`` trait in it.\n\n```php\nclass User extends Authenticatable\n{\n    use Filterable;\n    .\n    .\n    .\n    \n```\nThen add related filters that you created. It should be static property:\n\n```php\nuse App\\Filters\\Active;\nuse App\\Filters\\City;\n\nclass User extends Authenticatable\n{\n    use Filterable;\n \n    protected static $filters = [\n        Active::class,\n        City::class\n    ];\n\n    .\n    .\n    .\n\n```\n\nEvery thing is ready. just use it in your queries:\n```php\nUser::filter()-\u003eget();\n```\n\n\nif you want to specify some filter you can pass them thought this method:\n```php\nUser::filterThrough([City::class])-\u003eget();\n```\n\nIt's good to mention that this package works with query string. Ex:\n```php\nRoute::get('/?country=germany', function() {\n    return User-\u003efilter()-\u003eget();\n});\n```\nSo you should pass params through the url. The default query name is filter class name. Of course you can change the filters query name by:\n \n```php\n\nclass CountryFilter extends FilterContract\n{\n    public static $queryName = 'country';\n    .\n    .\n    .\n\n```\n\nAlso, you can set rules on your query string parameters:\n```php\n\nclass Active extends FilterContract\n{\n \n    public function rules()\n    {\n        return [\n            'required',\n            Rule::in(['true', 'false']),\n        ];\n    }\n```\nMore than that sometimes we would like cast query string value. So:\n\n```php\n\nclass Active extends FilterContract\n{\n    \n    protected $cast = 'boolean';\n\n    public function rules()\n    {\n        return [\n            //\n        ];\n    }\n```\n\n### Testing\n\n``` bash\ncomposer test\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email i.jedari@gmail.com instead of using the issue tracker.\n\n## Credits\n\n- [Mahdi Jedari](https://github.com/mjedari)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmjedari%2Flarafilter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmjedari%2Flarafilter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmjedari%2Flarafilter/lists"}