{"id":23512760,"url":"https://github.com/baethon/eloquent-searchable-scope","last_synced_at":"2026-05-02T02:34:53.930Z","repository":{"id":62491135,"uuid":"336520440","full_name":"baethon/eloquent-searchable-scope","owner":"baethon","description":"A dead-simple model scope used for searching","archived":false,"fork":false,"pushed_at":"2023-08-16T14:38:00.000Z","size":72,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"2.x","last_synced_at":"2025-10-04T05:56:18.830Z","etag":null,"topics":["eloquent","eloquent-models","laravel","scope","search"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baethon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2021-02-06T11:21:17.000Z","updated_at":"2023-05-04T18:35:11.000Z","dependencies_parsed_at":"2025-05-13T19:29:21.313Z","dependency_job_id":"3a254a08-7264-436a-9050-621954a4859a","html_url":"https://github.com/baethon/eloquent-searchable-scope","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/baethon/eloquent-searchable-scope","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Feloquent-searchable-scope","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Feloquent-searchable-scope/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Feloquent-searchable-scope/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Feloquent-searchable-scope/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baethon","download_url":"https://codeload.github.com/baethon/eloquent-searchable-scope/tar.gz/refs/heads/2.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Feloquent-searchable-scope/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32521108,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-02T01:12:54.858Z","status":"online","status_checked_at":"2026-05-02T02:00:05.923Z","response_time":132,"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":["eloquent","eloquent-models","laravel","scope","search"],"created_at":"2024-12-25T13:19:22.649Z","updated_at":"2026-05-02T02:34:53.876Z","avatar_url":"https://github.com/baethon.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# baethon/eloquent-searchable-scope\n\nAn Eloquent scope for building a search query using LIKE statements, which also supports searching using relations.\n\n```php\n$foundPosts = Post::query()\n    -\u003esearch($search)\n    -\u003eget();\n```\n\n# Installation\n\n```\ncomposer require baethon/eloquent-searchable-scope\n```\n\n# Model configuration\n\nImport `Searchable` trait and use it in model:\n\n```php\nnamespace App\\Models;\n\nuse Baethon\\Laravel\\Scopes\\Searchable;\n\nclass Post extends Model\n{\n    use Searchable;\n}\n```\n\nTrait requires defining the `getSearchableOptions()` method:\n\n```php\nnamespace App\\Models;\n\nuse Baethon\\Laravel\\Scopes\\Searchable;\nuse Baethon\\Laravel\\Scopes\\SearchableOptions;\n\nclass Post extends Model\n{\n    use Searchable;\n    \n    public function getSearchableOptions(): SearchableOptions\n    {\n        return SearchableOptions::defaults()\n            -\u003efields(['topic', 'text', 'user.email'];\n    }\n}\n```\n\nNote: `user.email` refers to `user` relation. It has to be defined in the model.\n\n## Available Options\n\nThe `SearchableOptions` provides the ability to customize the search functionality in a few ways:\n\n- `breakToWords()` - splits the search term into words and searches against each of them.\n- `minTermLength(int $minLength)` - rejects any string/word that is shorter than the specified number of characters.\n- `fields(array $fields)` - specifies the fields to be used in the search.\n\nThe `SearchableOptions::defaults()` is equivalent of:\n\n```php\n(new SearchableOptions)-\u003eminTermLength(3);\n```\n\n## Overloading search options\n\nWhen using the `search()` scope, it is possible to define the searchable fields.\n\n```php\n$foundPosts = Post::query()\n    -\u003esearch($search, [\n        'title',\n    ])\n    -\u003eget();\n```\n\nor, pass custom options object:\n\n```php\n$foundPosts = Post::query()\n    -\u003esearch($search, SearchableOptions::defaults()-\u003efields(['title'])\n    -\u003eget();\n```\n\nIf passing a custom options object, ensure that the searchable fields are defined.\n\n# Nothing new here!\n\nThe idea for this scope has been previously discussed in various places, such as [🔗 here](https://freek.dev/1182-searching-models-using-a-where-like-query-in-laravel) and [🔗 here](https://laravel-tricks.com/tricks/eloquents-dynamic-scope-search-trait). However, since it can be difficult to locate these resources every time one needs them, I have created a package that simplifies the installation process. It is important to note that this package does not introduce any novel concepts.\n\n# Testing\n\n```\ncomposer test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaethon%2Feloquent-searchable-scope","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaethon%2Feloquent-searchable-scope","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaethon%2Feloquent-searchable-scope/lists"}