{"id":15107140,"url":"https://github.com/infinitypaul/laravel-database-filter","last_synced_at":"2025-04-13T15:12:20.483Z","repository":{"id":41971440,"uuid":"252306490","full_name":"infinitypaul/laravel-database-filter","owner":"infinitypaul","description":"Need to filter database results with a query string? Here's a beautiful, easy to extend laravel package to keep your code super tidy.","archived":false,"fork":false,"pushed_at":"2024-08-20T20:37:53.000Z","size":37,"stargazers_count":24,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-13T15:12:14.971Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://still-hollows-19731.herokuapp.com/designs","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/infinitypaul.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-01T23:09:53.000Z","updated_at":"2025-02-26T00:28:23.000Z","dependencies_parsed_at":"2023-12-11T02:29:47.586Z","dependency_job_id":"e5cce2e2-19ce-4ce2-be9c-e88be8d95fe6","html_url":"https://github.com/infinitypaul/laravel-database-filter","commit_stats":{"total_commits":31,"total_committers":2,"mean_commits":15.5,"dds":"0.032258064516129004","last_synced_commit":"3ddd9e58e990a7c27ea22dabfda995f5aac5ac65"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infinitypaul%2Flaravel-database-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infinitypaul%2Flaravel-database-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infinitypaul%2Flaravel-database-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infinitypaul%2Flaravel-database-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/infinitypaul","download_url":"https://codeload.github.com/infinitypaul/laravel-database-filter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248732488,"owners_count":21152852,"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":[],"created_at":"2024-09-25T21:04:44.147Z","updated_at":"2025-04-13T15:12:20.456Z","avatar_url":"https://github.com/infinitypaul.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Database Filter\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/infinitypaul/laravel-database-filter.svg?style=flat-square)](https://packagist.org/packages/infinitypaul/laravel-database-filter)\n[![Build Status](https://img.shields.io/travis/infinitypaul/laravel-database-filter/master.svg?style=flat-square)](https://travis-ci.org/infinitypaul/laravel-database-filter)\n[![Quality Score](https://img.shields.io/scrutinizer/g/infinitypaul/laravel-database-filter.svg?style=flat-square)](https://scrutinizer-ci.com/g/infinitypaul/laravel-database-filter)\n[![Total Downloads](https://img.shields.io/packagist/dt/infinitypaul/laravel-database-filter.svg?style=flat-square)](https://packagist.org/packages/infinitypaul/laravel-database-filter)\n\n  Ever been stuck with filtering a database table with lots of GET parameters?  I can only imagine how bulky your code will be. Relax, the easiest solution to this problem is right here.     Let's say we have to query our records table with the following get parameters\n  \n  \u003ehttps://www.example.com/records?dept=csc\u0026level=200\u0026grade=A\n  \n  \n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require infinitypaul/laravel-database-filter\n```\n\nThe package will automatically register itself, but if your laravel versions is \u003c 5.5 you will need to add Infinitypaul\\LaravelDatabaseFilter\\LaravelDatabaseFilterServiceProvider::class, service provider under your config/app.php file.\n\n## Usage\nOnce the package is installed, an artisan command is available to you.\n\n```bash\nphp artisan make:filter \n```\nWe would be working with the records table below:\n\n| Id  | Department | Level | Score | Grade|\n| --- | -----------| ------| ------| -----|\n| 1   | csc        | 200  | 68    | b     |\n| 2   | physics    | 100  | 90    | a     |\n| 3   | csc        | 100  | 90    | a     |\n| 4   | physics    | 200  | 60    | b     |\n| 5   | csc        | 200  | 80    | a     |\n\n The Filtering be done in 6 simple steps. Perfect right! Let's begin:\n\n### * Step 1\n\nCreate a mother filter class, this is where all filters will be recorded. This can be created with this one line of code: \n\n```bash\nphp artisan make:filter RecordFilter --model\n``` \n This package will generate a new PHP file RecordFilter.php under app/Filters folder. This is where all other filters will be created.. It wil look like this\n\n``` php\n\u003c?php\n\nnamespace App\\Filters;\n\nuse Infinitypaul\\LaravelDatabaseFilter\\Abstracts\\FiltersAbstract;\n\nclass RecordFilter extends FiltersAbstract {\n        protected $filters = [];\n}\n\n```\n### * Step 2:\n\nSince we are working with the records table, we will be working on the Record model. So open your Record model and include the following:\n    * The Filter Trait\n    * The Record Filter Class\n\n```php\n\n\nnamespace App;\n\nuse App\\Filters\\RecordFilter;\nuse Infinitypaul\\LaravelDatabaseFilter\\Traits\\filterTrait;\n\n\nclass Course extends Model\n{\n    use filterTrait;\n\n    protected $filter = RecordFilter::class; //mother filter class\n\n}\n```\n\n### * Step 3:\n\nWe would be filtering with 3 parameters (dept, level, grade):\n\nLet's create filter classes for each filter\n\n```bash\nphp artisan make:filter DeptFilter\nphp artisan make:filter LevelFilter\nphp artisan make:filter GradeFilter\n``` \n\nThe above commands will also generate a new PHP file under app/Filters folder with the name DebtFilter.php, LevelFilter.php and GradeFilter.php which will look like below.\n\n```php\n\nnamespace App\\Filters;\n\nuse Illuminate\\Database\\Eloquent\\Builder;\n\nuse Infinitypaul\\LaravelDatabaseFilter\\Abstracts\\FilterAbstract;\n\nclass DebtFilter extends FilterAbstract\n{\n    \n        public function mappings ()\n        {\n            return [];\n        }\n\n        \n        public function filter(Builder $builder, $value)\n        {\n            return $builder;\n        }\n}\n\n\n```\n\n Well Done!!! Let's move to the next step\n \n### * Step 4:\n\nNow we write our logic in each class:\n\nIn our DeptFilter class, we write the following:\n\n```php\n\n  namespace App\\Filters;\n\n            use Illuminate\\Database\\Eloquent\\Builder;\n\n            use Infinitypaul\\LaravelDatabaseFilter\\Abstracts\\FilterAbstract;\n\n            class DeptFilter extends FilterAbstract\n            {\n\n                    public function mappings ()\n                    {\n                        return [];\n                    }\n\n                    public function filter(Builder $builder, $value)\n                    {\n                        return $builder-\u003ewhere('dept', $value);\n                    }\n            }\n```\n\nIn our LevelFilter class, we write the following:\n\n```php\n\n            namespace App\\Filters;\n\n            use Illuminate\\Database\\Eloquent\\Builder;\n\n            use Infinitypaul\\LaravelDatabaseFilter\\Abstracts\\FilterAbstract;\n\n            class LevelFilter extends FilterAbstract\n            {\n\n                    public function mappings ()\n                    {\n                        return [];\n                    }\n\n                    public function filter(Builder $builder, $value)\n                    {\n                        return $builder-\u003ewhere('level', $value);\n                    }\n            }\n\n```\n\nIn our GradeFilter class, we write the following:\n\n```php\n\n            namespace App\\Filters;\n\n            use Illuminate\\Database\\Eloquent\\Builder;\n\n            use Infinitypaul\\LaravelDatabaseFilter\\Abstracts\\FilterAbstract;\n\n            class GradeFilter extends FilterAbstract\n            {\n\n                    public function mappings ()\n                    {\n                        return [];\n                    }\n\n                    public function filter(Builder $builder, $value)\n                    {\n                        return $builder-\u003ewhere('grade', $value);\n                    }\n            }\n\n```\n\nThe filter method takes in our conditions or whatever  checks against the database\n\n Bravo on completing that. Moving on!!!\n \n \n### * Step 5:\n \n We write our search parameters against the filter class. Let's open up our mother filter class - RecordFilter to register all the conditions we have generated. \n \n ```php\n \n             namespace App\\Filters;\n \n             use Infinitypaul\\LaravelDatabaseFilter\\Abstracts\\FiltersAbstract;\n \n             class RecordFilter extends FiltersAbstract {\n                     protected $filters = [\n                         'dept' =\u003e DeptFilter::class,\n                         'level' =\u003e LevelFilter::class,\n                         'grade' =\u003e GradeFilter::class\n                     ];\n             }\n \n ```\n\nThe $Filter array key is will be the query params.\n\n### * Step 6:\n\nAssuming we have a controller RecordController, we can create a function to get our record:\n\n```php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Course;\nuse App\\Filters\\AccessFilter;\nuse Illuminate\\Http\\Request;\n\nclass RecordController extends Controller\n{\n    public function index(Request $request){\n        return Course::filter($request)-\u003eget();\n    }\n}\n\n```\n\nWe are all done!!!\n\nWhen we enter the following into our browser\n\n\u003e https://www.example.com/records?dept=csc\u0026level=200\u0026grade=a\n\nWe will get the following result:\n\n```\n\n            {\n                \"data\": [\n                    {\n                        \"id\": 5,\n                        \"department\": \"csc\",\n                        \"level\": 200,\n                        \"score\": \"80\",\n                        \"grade\": \"a\"\n                    }\n                ]\n            }\n\n```\n### Tweak Time\n\nTo be strict about query params input, we can use the mapping method or leave empty for free entry.\n\nLet's say when returning the data, we want all department with csc to return as CSC. We return values to the mappings function in the DeptFilter class as shown below:\n\n```php\n\nnamespace App\\Filters;\n\nuse Illuminate\\Database\\Eloquent\\Builder;\n\nuse Infinitypaul\\LaravelDatabaseFilter\\Abstracts\\FilterAbstract;\n\nclass DeptFilter extends FilterAbstract\n{\n    \n        public function mappings ()\n        {\n            return [\n                'csc' =\u003e 'CSC'\n            ];\n        }\n\npublic function filter(Builder $builder, $value)\n                    {\n                        return $builder-\u003ewhere('dept', $value);\n                    }\n\n```\n\nWith the above setup, once csc is entered in your query params, it will return CSC as value.\n\nLastly, You can add local scoped filter() by passing an array of filter into the filter scope.\n\n\n```php\n  public function index(Request $request){\n        return Record::filter($request, ['score' =\u003e DifficultyFilter::class])-\u003eget();\n    }\n```\n\nYou can also override your filter property from your controller in this manner\n\n```php\n  public function course(Request $request){\n        return Record::filter($request, PaulFilter::class, ['score' =\u003e DifficultyFilter::class])-\u003eget();\n    }\n```\n\nSo instead of your filter to use the RecordFilter it will make use of PaulFilter\n\n\n\n### Bug \u0026 Features\n\nIf you have spotted any bugs, or would like to request additional features from the library, please file an issue via the Issue Tracker on the project's Github page: [https://github.com/infinitypaul/laravel-database-filter/issues](https://github.com/infinitypaul/laravel-database-filter/issues).\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email infinitypaul@live.com instead of using the issue tracker.\n\n## How can I thank you?\n\nWhy not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word!\n\nDon't forget to [follow me on twitter](https://twitter.com/infinitypaul)!\n\nThanks!\nEdward Paul.\n\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finfinitypaul%2Flaravel-database-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finfinitypaul%2Flaravel-database-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finfinitypaul%2Flaravel-database-filter/lists"}