{"id":13396212,"url":"https://github.com/nicolaslopezj/searchable","last_synced_at":"2025-05-14T02:04:53.214Z","repository":{"id":17936075,"uuid":"20911895","full_name":"nicolaslopezj/searchable","owner":"nicolaslopezj","description":"A php trait to search laravel models","archived":false,"fork":false,"pushed_at":"2023-03-17T13:03:08.000Z","size":116,"stargazers_count":2009,"open_issues_count":107,"forks_count":293,"subscribers_count":72,"default_branch":"master","last_synced_at":"2025-04-10T16:48:05.576Z","etag":null,"topics":["eloquent-models","laravel","php","prioritizing-matches","search"],"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/nicolaslopezj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2014-06-17T05:53:45.000Z","updated_at":"2025-04-08T12:08:28.000Z","dependencies_parsed_at":"2023-01-13T19:34:50.812Z","dependency_job_id":"4e380a26-eb76-407a-ba1a-b23b2d0745b3","html_url":"https://github.com/nicolaslopezj/searchable","commit_stats":{"total_commits":116,"total_committers":55,"mean_commits":2.109090909090909,"dds":0.7155172413793103,"last_synced_commit":"b1d24ea45f75e36a11a093efa46fe44c06dd10b7"},"previous_names":[],"tags_count":55,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicolaslopezj%2Fsearchable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicolaslopezj%2Fsearchable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicolaslopezj%2Fsearchable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicolaslopezj%2Fsearchable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nicolaslopezj","download_url":"https://codeload.github.com/nicolaslopezj/searchable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254052671,"owners_count":22006716,"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":["eloquent-models","laravel","php","prioritizing-matches","search"],"created_at":"2024-07-30T18:00:42.463Z","updated_at":"2025-05-14T02:04:53.192Z","avatar_url":"https://github.com/nicolaslopezj.png","language":"PHP","funding_links":[],"categories":["Popular Packages","PHP","Paquetes utiles","Packages","数据库( Database )"],"sub_categories":["Database/Eloquent/Models"],"readme":"Searchable, a search trait for Laravel\n==========================================\n\nSearchable is a trait for Laravel 4.2+ and Laravel 5.0 that adds a simple search function to Eloquent Models.\n\nSearchable allows you to perform searches in a table giving priorities to each field for the table and it's relations.\n\nThis is not optimized for big searches, but sometimes you just need to make it simple (Although it is not slow).\n\n# Installation\n\nSimply add the package to your `composer.json` file and run `composer update`.\n\n```\n\"nicolaslopezj/searchable\": \"1.*\"\n```\n\n# Usage\n\nAdd the trait to your model and your search rules.\n\n```php\nuse Nicolaslopezj\\Searchable\\SearchableTrait;\n\nclass User extends \\Eloquent\n{\n    use SearchableTrait;\n\n    /**\n     * Searchable rules.\n     *\n     * @var array\n     */\n    protected $searchable = [\n        /**\n         * Columns and their priority in search results.\n         * Columns with higher values are more important.\n         * Columns with equal values have equal importance.\n         *\n         * @var array\n         */\n        'columns' =\u003e [\n            'users.first_name' =\u003e 10,\n            'users.last_name' =\u003e 10,\n            'users.bio' =\u003e 2,\n            'users.email' =\u003e 5,\n            'posts.title' =\u003e 2,\n            'posts.body' =\u003e 1,\n        ],\n        'joins' =\u003e [\n            'posts' =\u003e ['users.id','posts.user_id'],\n        ],\n    ];\n\n    public function posts()\n    {\n        return $this-\u003ehasMany('Post');\n    }\n\n}\n```\n\nNow you can search your model.\n\n```php\n// Simple search\n$users = User::search($query)-\u003eget();\n\n// Search and get relations\n// It will not get the relations if you don't do this\n$users = User::search($query)\n            -\u003ewith('posts')\n            -\u003eget();\n```\n\n\n## Search Paginated\n\nAs easy as laravel default queries\n\n```php\n// Search with relations and paginate\n$users = User::search($query)\n            -\u003ewith('posts')\n            -\u003epaginate(20);\n```\n\n## Mix queries\n\nSearch method is compatible with any eloquent method. You can do things like this:\n\n```php\n// Search only active users\n$users = User::where('status', 'active')\n            -\u003esearch($query)\n            -\u003epaginate(20);\n```\n\n## Custom Threshold\n\nThe default threshold for accepted relevance is the sum of all attribute relevance divided by 4.\nTo change this value you can pass in a second parameter to search() like so:\n\n```php\n// Search with lower relevance threshold\n$users = User::where('status', 'active')\n            -\u003esearch($query, 0)\n            -\u003epaginate(20);\n```\n\nThe above, will return all users in order of relevance.\n\n## Entire Text search\n\nBy default, multi-word search terms are split and Searchable searches for each word individually. Relevance plays a role in prioritizing matches that matched on multiple words. If you want to prioritize matches that include the multi-word search (thus, without splitting into words) you can enable full text search by setting the third value to true. Example:\n\n```php\n// Prioritize matches containing \"John Doe\" above matches containing only \"John\" or \"Doe\".\n$users = User::search(\"John Doe\", null, true)-\u003eget();\n```\n\nIf you explicitly want to search for full text matches only, you can disable multi-word splitting by setting the fourth parameter to true.\n\n```php\n// Do not include matches that only matched \"John\" OR \"Doe\".\n$users = User::search(\"John Doe\", null, true, true)-\u003eget();\n```\n\n# How does it work?\n\nSearchable builds a query that search through your model using Laravel's Eloquent.\nHere is an example query\n\n#### Eloquent Model:\n```php\nuse Nicolaslopezj\\Searchable\\SearchableTrait;\n\nclass User extends \\Eloquent\n{\n    use SearchableTrait;\n\n    /**\n     * Searchable rules.\n     *\n     * @var array\n     */\n    protected $searchable = [\n        'columns' =\u003e [\n            'first_name' =\u003e 10,\n            'last_name' =\u003e 10,\n            'bio' =\u003e 2,\n            'email' =\u003e 5,\n        ],\n    ];\n\n}\n```\n\n#### Search:\n```php\n$search = User::search('Sed neque labore', null, true)-\u003eget();\n```\n\n#### Result:\n```sql\nselect `users`.*, \n\n-- If third parameter is set as true, it will check if the column starts with the search\n-- if then it adds relevance * 30\n-- this ensures that relevant results will be at top\n(case when first_name LIKE 'Sed neque labore%' then 300 else 0 end) + \n\n-- For each column you specify makes 3 \"ifs\" containing \n-- each word of the search input and adds relevace to \n-- the row\n\n-- The first checks if the column is equal to the word,\n-- if then it adds relevance * 15\n(case when first_name LIKE 'Sed' || first_name LIKE 'neque' || first_name LIKE 'labore' then 150 else 0 end) + \n\n-- The second checks if the column starts with the word,\n-- if then it adds relevance * 5\n(case when first_name LIKE 'Sed%' || first_name LIKE 'neque%' || first_name LIKE 'labore%' then 50 else 0 end) + \n\n-- The third checks if the column contains the word, \n-- if then it adds relevance * 1\n(case when first_name LIKE '%Sed%' || first_name LIKE '%neque%' || first_name LIKE '%labore%' then 10 else 0 end) + \n\n-- Repeats with each column\n(case when last_name LIKE 'Sed' || last_name LIKE 'neque' || last_name LIKE 'labore' then 150 else 0 end) + \n(case when last_name LIKE 'Sed%' || last_name LIKE 'neque%' || last_name LIKE 'labore%' then 50 else 0 end) +\n(case when last_name LIKE '%Sed%' || last_name LIKE '%neque%' || last_name LIKE '%labore%' then 10 else 0 end) + \n\n(case when bio LIKE 'Sed' || bio LIKE 'neque' || bio LIKE 'labore' then 30 else 0 end) + \n(case when bio LIKE 'Sed%' || bio LIKE 'neque%' || bio LIKE 'labore%' then 10 else 0 end) + \n(case when bio LIKE '%Sed%' || bio LIKE '%neque%' || bio LIKE '%labore%' then 2 else 0 end) + \n\n(case when email LIKE 'Sed' || email LIKE 'neque' || email LIKE 'labore' then 75 else 0 end) + \n(case when email LIKE 'Sed%' || email LIKE 'neque%' || email LIKE 'labore%' then 25 else 0 end) + \n(case when email LIKE '%Sed%' || email LIKE '%neque%' || email LIKE '%labore%' then 5 else 0 end) \n\nas relevance \nfrom `users` \ngroup by `id` \n\n-- Selects only the rows that have more than\n-- the sum of all attributes relevances and divided by 4\n-- Ej: (20 + 5 + 2) / 4 = 6.75\nhaving relevance \u003e 6.75 \n\n-- Orders the results by relevance\norder by `relevance` desc\n```\n\n## Contributing\n\nAnyone is welcome to contribute. Fork, make your changes, and then submit a pull request.\n\n[![Support via Gittip](https://rawgithub.com/twolfson/gittip-badge/0.2.0/dist/gittip.png)](https://gratipay.com/nicolaslopezj/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicolaslopezj%2Fsearchable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicolaslopezj%2Fsearchable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicolaslopezj%2Fsearchable/lists"}