{"id":23512767,"url":"https://github.com/baethon/laravel-criteria","last_synced_at":"2025-09-19T12:11:49.881Z","repository":{"id":62491195,"uuid":"139031834","full_name":"baethon/laravel-criteria","owner":"baethon","description":"Minimal interface for criteria pattern","archived":false,"fork":false,"pushed_at":"2025-01-09T01:17:07.000Z","size":62,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-28T00:26:58.025Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/baethon.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-06-28T14:47:39.000Z","updated_at":"2025-03-20T16:50:17.000Z","dependencies_parsed_at":"2023-02-16T21:45:30.819Z","dependency_job_id":null,"html_url":"https://github.com/baethon/laravel-criteria","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/baethon/laravel-criteria","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-criteria","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-criteria/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-criteria/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-criteria/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baethon","download_url":"https://codeload.github.com/baethon/laravel-criteria/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-criteria/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274679973,"owners_count":25330129,"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-09-11T02:00:13.660Z","response_time":74,"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":[],"created_at":"2024-12-25T13:19:23.975Z","updated_at":"2025-09-19T12:11:49.857Z","avatar_url":"https://github.com/baethon.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# baethon/laravel-criteria\n\nMinimal implementation of [criteria pattern](https://www.tutorialspoint.com/design_pattern/filter_pattern.htm) for Laravel.\n\n## What is criteria?\n\nCriteria is an implementation of `\\Baethon\\LaravelCriteria\\CriteriaInterface` which is responsible for only one thing - modify Laravels query in a specified way.\n\nThis allows to decouple and re-use query modifiers.\n\nCriteria are very similar to [Eloquent scopes](https://laravel.com/docs/eloquent#query-scopes). The main difference is that they can be applied to different models across the application.\n\n## Installation\n\n```php\ncomposer require baethon/laravel-criteria\n```\n\n## Requirements\n\n* PHP \u003e= 7.1\n\n### Laravel compatibility\n\nThis library has no dependencies to Laravel itself. It should work with all versions of Laravel.\n\n## Usage\n\nFirst, you need to create an implementation of `CriteriaInterface`:\n\n```php\n\u003c?php\n\nuse Baethon\\LaravelCriteria\\CriteriaInterface;\n\nclass CompareCriteria implements CriteriaInterface\n{\n    private $field;\n\n    private $value;\n\n    public function __construct(string $field, string $value)\n    {\n        $this-\u003efield = $field;\n        $this-\u003evalue = $value;\n    }\n\n    public function apply($query)\n    {\n        $query-\u003ewhere($this-\u003efield, $this-\u003evalue);\n    }\n}\n```\n\nNow, you can *apply* it to the query:\n\n```php\n$query = User::query();\n\n(new CompareCriteria('name', 'Jon'))-\u003eapply($query);\n\n$jon = $query-\u003efirst();\n```\n\n### AppliesCriteria trait\n\nTo simplify things it's possible to use `AppliesCriteria` trait in a model.\n\n```php\nuse Baethon\\LaravelCriteria\\Traits\\AppliesCriteria;\n\nclass User extends Model\n{\n    // model body stripped for better readability\n\n    use AppliesCriteria;\n}\n\n$jon = User::query()\n    -\u003eapply(new CompareCriteria('name', 'Jon'))\n    -\u003efirst();\n```\n\n### Collections\n\nThe package provides collections which allow applying multiple criteria at once.\n\nTo apply group of criteria use `\\Baethon\\LaravelCriteria\\Collections\\CriteriaCollection`:\n\n```php\n$jonSnow = User::query()\n    -\u003eapply(CriteriaCollection::create([\n        new CompareCriteria('name', 'Jon'),\n        new CompareCriteria('lastname', 'Snow'),\n    ]))\n    -\u003efirst();\n\n// same result, without CriteriaCollection\n$jonSnow = User::query()\n    -\u003eapply(new CompareCriteria('name', 'Jon'))\n    -\u003eapply(new CompareCriteria('lastname', 'Snow'))\n    -\u003efirst();\n```\n\nIf you need to be sure that all criteria will be fulfilled you can use `CriteriaCollection::allOf()`:\n\n```php\nUser::query()\n    -\u003eapply(CriteriaCollection::allOf([\n        new CompareCriteria('name', 'Jon'),\n        new CompareCriteria('lastname', 'Snow'),\n    ]));\n\n// same as\nUser::query()\n    -\u003ewhere(function ($query) {\n        $query-\u003eapply(new CompareCriteria('name', 'Jon'))\n            -\u003eapply(new CompareCriteria('lastname', 'Snow'));\n    });\n```\n\nAlso, you can group criteria using logical `OR` join:\n\n```php\nUser::query()\n    -\u003eapply(CriteriaCollection::oneOf([\n        new CompareCriteria('name', 'Jon'),\n        new CompareCriteria('lastname', 'Snow'),\n    ]));\n\n// same as\nUser::query()\n    -\u003ewhere(function ($query) {\n        $query-\u003ewhere('name', 'Jon')\n            -\u003eorWhere('lastname', 'Snow');\n    });\n```\n\n## Testing\n\nRun tests with:\n\n```bash\n./vendor/bin/phpunit\n```\n\n## Changelog\n\nPlease see [CHANGELOG](https://github.com/baethon/laravel-criteria/blob/master/CHANGELOG.md) for more information what has changed recently.\n\n## License\n\nThe MIT License (MIT). Please see License File for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaethon%2Flaravel-criteria","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaethon%2Flaravel-criteria","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaethon%2Flaravel-criteria/lists"}