{"id":13462033,"url":"https://github.com/spatie/laravel-searchable","last_synced_at":"2025-05-13T17:11:16.557Z","repository":{"id":37430958,"uuid":"160661570","full_name":"spatie/laravel-searchable","owner":"spatie","description":"Pragmatically search through models and other sources","archived":false,"fork":false,"pushed_at":"2025-02-25T16:00:32.000Z","size":142,"stargazers_count":1339,"open_issues_count":0,"forks_count":115,"subscribers_count":22,"default_branch":"main","last_synced_at":"2025-04-18T15:16:22.016Z","etag":null,"topics":["laravel","php","search"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source","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/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","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,"zenodo":null},"funding":{"custom":"https://spatie.be/open-source/support-us"}},"created_at":"2018-12-06T10:55:46.000Z","updated_at":"2025-04-15T14:45:11.000Z","dependencies_parsed_at":"2024-01-20T18:06:26.703Z","dependency_job_id":"4f3b760a-7958-498a-aff3-554ae162ce59","html_url":"https://github.com/spatie/laravel-searchable","commit_stats":{"total_commits":156,"total_committers":29,"mean_commits":5.379310344827586,"dds":0.6987179487179487,"last_synced_commit":"14b65df0448e88d7fee46bb3e98707a515f92cd1"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-searchable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-searchable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-searchable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-searchable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-searchable/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249947526,"owners_count":21349819,"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":["laravel","php","search"],"created_at":"2024-07-31T12:00:37.600Z","updated_at":"2025-04-23T23:19:38.842Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://spatie.be/open-source/support-us"],"categories":["Uncategorized","PHP"],"sub_categories":["Uncategorized"],"readme":"# Laravel Searchable\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-searchable.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-searchable)\n[![run-tests](https://github.com/spatie/laravel-searchable/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-searchable/actions/workflows/run-tests.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-searchable.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-searchable)\n\nThis package makes it easy to get structured search from a variety of sources. Here's an example where we search through some models. We already did some small preparation on the models themselves.\n\n```php\n$searchResults = (new Search())\n   -\u003eregisterModel(User::class, 'name')\n   -\u003eregisterModel(BlogPost::class, 'title')\n   -\u003esearch('john');\n```\n\nThe search will be performed case insensitive. `$searchResults` now contains all `User` models that contain `john` in the `name` attribute and `BlogPost`s that contain 'john' in the `title` attribute.\n\nIn your view you can now loop over the search results:\n\n```blade\n\u003ch1\u003eSearch\u003c/h1\u003e\n\nThere are {{ $searchResults-\u003ecount() }} results.\n\n@foreach($searchResults-\u003egroupByType() as $type =\u003e $modelSearchResults)\n   \u003ch2\u003e{{ $type }}\u003c/h2\u003e\n   \n   @foreach($modelSearchResults as $searchResult)\n       \u003cul\u003e\n            \u003cli\u003e\u003ca href=\"{{ $searchResult-\u003eurl }}\"\u003e{{ $searchResult-\u003etitle }}\u003c/a\u003e\u003c/li\u003e\n       \u003c/ul\u003e\n   @endforeach\n@endforeach\n```\n\nIn this example we used models, but you can easily add a search aspect for an external API, list of files or an array of values.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-searchable.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-searchable)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/laravel-searchable\n```\n\n## Usage\n\n### Preparing your models\n\nIn order to search through models you'll have to let them implement the `Searchable` interface.\n\n```php\nnamespace Spatie\\Searchable;\n\ninterface Searchable\n{\n    public function getSearchResult(): SearchResult;\n}\n```\n\nYou'll only need to add a `getSearchResult` method to each searchable model that must return an instance of `SearchResult`. Here's how it could look like for a blog post model.\n\n```php\nuse Spatie\\Searchable\\Searchable;\nuse Spatie\\Searchable\\SearchResult;\n\nclass BlogPost extends Model implements Searchable\n{\n     public function getSearchResult(): SearchResult\n     {\n        $url = route('blogPost.show', $this-\u003eslug);\n     \n         return new \\Spatie\\Searchable\\SearchResult(\n            $this,\n            $this-\u003etitle,\n            $url\n         );\n     }\n}\n```\n\n### Searching models\n\nWith the models prepared you can search them like this:\n\n```php\n$searchResults = (new Search())\n   -\u003eregisterModel(User::class, 'name')\n   -\u003esearch('john');\n```\n\nThe search will be performed case insensitive. `$searchResults` now contains all `User` models that contain `john` in the `name` attribute.\n\nYou can also pass multiple attributes to search through:\n\n```php\n// use multiple model attributes\n\n$searchResults = (new Search())\n   -\u003eregisterModel(User::class, 'first_name', 'last_name')\n   -\u003esearch('john');\n   \n// or use an array of model attributes\n\n$searchResults = (new Search())\n   -\u003eregisterModel(User::class, ['first_name', 'last_name'])\n   -\u003esearch('john');\n```\n\nTo get fine grained control you can also use a callable. This way you can also search for exact matches, apply scopes, eager load relationships, or even filter your query like you would using the query builder.\n\n```php\n$search = (new Search())\n   -\u003eregisterModel(User::class, function(ModelSearchAspect $modelSearchAspect) {\n       $modelSearchAspect\n          -\u003eaddSearchableAttribute('name') // return results for partial matches on usernames\n          -\u003eaddExactSearchableAttribute('email') // only return results that exactly match the e-mail address\n          -\u003eactive()\n          -\u003ehas('posts')\n          -\u003ewith('roles');\n});\n```\n\n### Creating custom search aspects\n\nYou are not limited to only registering basic models as search aspects. You can easily create your own, custom search aspects by extending the `SearchAspect` class.\n\nConsider the following custom search aspect to search an external API:\n\n```php\nclass OrderSearchAspect extends SearchAspect\n{\n    public function getResults(string $term): Collection\n    {\n        return OrderApi::searchOrders($term);\n    }\n}\n```\n\nThis is how you can use it:\n\n```php\n$searchResults = (new Search())\n   -\u003eregisterAspect(OrderSearchAspect::class)\n   -\u003esearch('john');\n```\n\n### Limiting aspect results\n\nIt is possible to limit the amount of results returned by each aspect by calling `limitAspectResults` prior to performing the search.\n\n```php\n$searchResults = (new Search())\n    -\u003eregisterAspect(BlogPostAspect::class)\n    -\u003elimitAspectResults(50)\n    -\u003esearch('How To');\n```\n\n### Rendering search results\n\nHere's an example on rendering search results:\n\n```blade\n\u003ch1\u003eSearch\u003c/h1\u003e\n\nThere are {{ $searchResults-\u003ecount() }} results.\n\n@foreach($searchResults-\u003egroupByType() as $type =\u003e $modelSearchResults)\n   \u003ch2\u003e{{ $type }}\u003c/h2\u003e\n   \n   @foreach($modelSearchResults as $searchResult)\n       \u003cul\u003e\n            \u003ca href=\"{{ $searchResult-\u003eurl }}\"\u003e{{ $searchResult-\u003etitle }}\u003c/a\u003e\n       \u003c/ul\u003e\n   @endforeach\n@endforeach\n```\n\nYou can customize the `$type` by adding a public property `$searchableType` on your model or custom search aspect\n\n```php\nclass BlogPost extends Model implements Searchable\n{\n    public $searchableType = 'custom named aspect';\n}\n```\n\n### Testing\n\n```bash\ncomposer test\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n### Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Credits\n\n- [Alex Vanderbist](https://github.com/AlexVanderbist)\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\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%2Fspatie%2Flaravel-searchable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-searchable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-searchable/lists"}