{"id":41602780,"url":"https://github.com/mayoz/eloquent-filterable","last_synced_at":"2026-01-24T10:52:17.225Z","repository":{"id":29518057,"uuid":"33056493","full_name":"mayoz/eloquent-filterable","owner":"mayoz","description":"Manage Laravel 5+ Eloquent queries with filters.","archived":false,"fork":false,"pushed_at":"2015-11-06T21:01:51.000Z","size":200,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-26T23:55:26.449Z","etag":null,"topics":[],"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/mayoz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-29T00:59:24.000Z","updated_at":"2018-06-06T11:20:48.000Z","dependencies_parsed_at":"2022-09-03T20:32:14.530Z","dependency_job_id":null,"html_url":"https://github.com/mayoz/eloquent-filterable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/mayoz/eloquent-filterable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mayoz%2Feloquent-filterable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mayoz%2Feloquent-filterable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mayoz%2Feloquent-filterable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mayoz%2Feloquent-filterable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mayoz","download_url":"https://codeload.github.com/mayoz/eloquent-filterable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mayoz%2Feloquent-filterable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28725378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-24T10:24:43.181Z","status":"ssl_error","status_checked_at":"2026-01-24T10:24:36.112Z","response_time":89,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":[],"created_at":"2026-01-24T10:52:14.276Z","updated_at":"2026-01-24T10:52:17.216Z","avatar_url":"https://github.com/mayoz.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Eloquent Filterable\n\nWith this package, you can optimize query clauses calling before or after the filter. You can manage your queries from a single interface.\n\n## Installation\n\n[PHP](https://php.net/) 5.5.9+ or [HHVM](http://hhvm.com/), and [Composer](https://getcomposer.org/) are required.\n\nTo get the latest version of Eloquent Filterable, simply add the following line to the require block of your `composer.json` file:\n\n```php\n\"require\": {\n    \"mayoz/eloquent-filterable\": \"~2.0\"\n}\n```\n\nYou'll then need to run `composer install` or `composer update` to download it and have the autoloader updated. Or use to shortcut installed through terminal:\n\n```bash\ncomposer require mayoz/eloquent-filterable ~2.0\n```\n\n## Usage\n\n1. Create your query filters.\n2. Create your Eloquent models.\n3. Define `Filterable` trait and use the query filters in the Eloquent model.\n\n### Create Filters\n\nAll filters should be extend `Mayoz\\Filter\\Filter` abstract class. Thus, can be used `before` and `after` methods in your filters.\n\n#### The Before Method\n\nThe `before` method responsible to position the **head** of the WHERE clause of the query. For example; we need `published = 1` of query WHERE clause. However, this clause would like to work on before the other clauses.\n\n```php\n\u003c?php namespace App\\Filters;\n\nuse Mayoz\\Filter\\Filter;\n\nclass PublishedFilter extends Filter\n{\n\t/**\n\t * The first executable filter clause.\n\t *\n\t * @param  mixed  $query\n\t * @return void\n\t */\n\tpublic function before($query)\n\t{\n\t\t$query-\u003ewhere('published', '=', 1);\n\t}\n}\n```\n\n#### The After Method\n\nThe `after` method responsible to position the **end** of the WHERE clause of the query. For example, if need `status = 'active'` of query WHERE clause. However, this clause would like to work on after the other clauses.\n\n```php\n\u003c?php namespace App\\Filters;\n\nuse Mayoz\\Filter\\Filter;\n\nclass StatusActiveFilter extends Filter\n{\n\t/**\n\t * The last executable filter clause.\n\t *\n\t * @param  mixed  $query\n\t * @return void\n\t */\n\tpublic function after($query)\n\t{\n\t\t$query-\u003ewhere('status', '=', 'active');\n\t}\n}\n```\n\n### Create Model\n\nCreate your model file. If you want to manage queries add the `Filterable` trait your model file. And than, assign the all associative filters to `$filters` variable. Consider the following example:\n\n```php\n\u003c?php namespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Mayoz\\Filter\\Filterable;\n\nclass Post extends Model\n{\n    use Filterable;\n\n\t/**\n\t * The attributes that should be filter.\n\t *\n\t * @var array\n\t */\n\tprotected $filters = [\n\t\t'App\\Filters\\StatusActiveFilter',\n\t\t'App\\Filters\\PublishedFilter'\n\t];\n\n\t// other things...\n}\n```\n\n\u003e **Important:** The order of the filter is important when adding (if need multiple before or after filters) query clause. The before filters are added the head of the clause according to the reference sequence. Likewise, the after filters.\n\n### Debug\n\nNow the `Post` model is ready to use. You ready? Okay, we're testing the query. Don't forget to enable the [query logging](http://laravel.com/docs/5.1/database#listening-for-query-events) for examine the model queries.\n\n```php\n$model = \\App\\Post::where('views', '\u003e', 100)-\u003eget();\n```\n\nQuery logging output:\n\n```sql\nSELECT *\nFROM   `posts`\nWHERE  `published` = 1     # added by automatic filter.\n  AND  `views` \u003e 100       # user defined where clause\n  AND  `status` = 'active' # added by automatic filter.\n```\n\n## Why Use?\n\nFor example, you might want to show only the approved text of the visitors on the site. However, administrators can see them all. Create a new extended filter:\n\n```php\n\u003c?php namespace App\\Filters;\n\nuse Auth;\nuse Mayoz\\Filter\\Filter;\n\nclass StatusActiveFilter extends Filter\n{\n\t/**\n\t * The last executable filter clause.\n\t *\n\t * @param  mixed  $query\n\t * @return void\n\t */\n\tpublic function after($query)\n\t{\n\t\t# assume, the `users` table has `role` field.\n\t\tif (array_get(Auth::user(), 'role') != 'administrator')\n\t\t{\n\t\t\t$query-\u003ewhere('status', '=', 'active');\n\t\t}\n\t}\n}\n```\n\nHocus, pocus! Visitors will display only the active posts. But administrator display all. Ok?\n\n# Contributing\n\nLove innovation and simplicity. Please use issues all errors or suggestions reporting. Follow the steps for contributing:\n\n1. Fork the repo.\n2. Follow the [Laravel Coding Style](http://laravel.com/docs/master/contributions#coding-style).\n3. If necessary, check your changes with unittest.\n4. Commit all changes.\n5. Push your commit and create a new PR.\n6. Wait for merge the PR.\n\n# Unit Test\n\nPlease create your tests and check before PR. Use the command:\n\n```bash\n$ phpunit\n```\n\n# License\n\nEloquent Filterable is licensed under [The MIT License (MIT)](https://github.com/mayoz/eloquent-filterable/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmayoz%2Feloquent-filterable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmayoz%2Feloquent-filterable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmayoz%2Feloquent-filterable/lists"}