├── .github └── workflows │ ├── test-mysql.yml │ ├── test-pgsql.yml │ └── test-sqlite.yml ├── .styleci.yml ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── docker-compose-test.yml ├── hero.png ├── phpunit.mysql.xml.dist ├── phpunit.pgsql.xml.dist └── src └── LaravelNovaSearchable.php /.github/workflows/test-mysql.yml: -------------------------------------------------------------------------------- 1 | name: "Test MySQL" 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | test-php-73: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Start the test docker containers 15 | run: docker-compose -f docker-compose-test.yml up -d 16 | - name: Delete vendor 17 | run: docker-compose -f docker-compose-test.yml exec -T php_73 rm -rf vendor 18 | - name: Install composer dependencies 19 | run: docker-compose -f docker-compose-test.yml exec -T php_73 composer install 20 | - name: Run PHPUnit Tests 21 | run: docker-compose -f docker-compose-test.yml exec -T php_73 ./vendor/bin/phpunit --configuration phpunit.mysql.xml.dist 22 | test-php-74: 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: actions/checkout@v2 26 | - name: Start the test docker containers 27 | run: docker-compose -f docker-compose-test.yml up -d 28 | - name: Delete vendor 29 | run: docker-compose -f docker-compose-test.yml exec -T php_74 rm -rf vendor 30 | - name: Install composer dependencies 31 | run: docker-compose -f docker-compose-test.yml exec -T php_74 composer install 32 | - name: Run PHPUnit Tests 33 | run: docker-compose -f docker-compose-test.yml exec -T php_74 ./vendor/bin/phpunit --configuration phpunit.mysql.xml.dist 34 | test-php-80: 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v2 38 | - name: Start the test docker containers 39 | run: docker-compose -f docker-compose-test.yml up -d 40 | - name: Delete vendor 41 | run: docker-compose -f docker-compose-test.yml exec -T php_80 rm -rf vendor 42 | - name: Install composer dependencies 43 | run: docker-compose -f docker-compose-test.yml exec -T php_80 composer install 44 | - name: Run PHPUnit Tests 45 | run: docker-compose -f docker-compose-test.yml exec -T php_80 ./vendor/bin/phpunit --configuration phpunit.mysql.xml.dist 46 | -------------------------------------------------------------------------------- /.github/workflows/test-pgsql.yml: -------------------------------------------------------------------------------- 1 | name: "Test pgSQL" 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | test-php-73: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Start the test docker containers 15 | run: docker-compose -f docker-compose-test.yml up -d 16 | - name: Delete vendor 17 | run: docker-compose -f docker-compose-test.yml exec -T php_73 rm -rf vendor 18 | - name: Install composer dependencies 19 | run: docker-compose -f docker-compose-test.yml exec -T php_73 composer install 20 | - name: Run PHPUnit Tests 21 | run: docker-compose -f docker-compose-test.yml exec -T php_73 ./vendor/bin/phpunit --configuration phpunit.pgsql.xml.dist 22 | test-php-74: 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: actions/checkout@v2 26 | - name: Start the test docker containers 27 | run: docker-compose -f docker-compose-test.yml up -d 28 | - name: Delete vendor 29 | run: docker-compose -f docker-compose-test.yml exec -T php_74 rm -rf vendor 30 | - name: Install composer dependencies 31 | run: docker-compose -f docker-compose-test.yml exec -T php_74 composer install 32 | - name: Run PHPUnit Tests 33 | run: docker-compose -f docker-compose-test.yml exec -T php_74 ./vendor/bin/phpunit --configuration phpunit.pgsql.xml.dist 34 | test-php-80: 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v2 38 | - name: Start the test docker containers 39 | run: docker-compose -f docker-compose-test.yml up -d 40 | - name: Delete vendor 41 | run: docker-compose -f docker-compose-test.yml exec -T php_80 rm -rf vendor 42 | - name: Install composer dependencies 43 | run: docker-compose -f docker-compose-test.yml exec -T php_80 composer install 44 | - name: Run PHPUnit Tests 45 | run: docker-compose -f docker-compose-test.yml exec -T php_80 ./vendor/bin/phpunit --configuration phpunit.pgsql.xml.dist 46 | -------------------------------------------------------------------------------- /.github/workflows/test-sqlite.yml: -------------------------------------------------------------------------------- 1 | name: "Test SQLite" 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | test-php-73: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Start the test docker containers 15 | run: docker-compose -f docker-compose-test.yml up -d 16 | - name: Delete vendor 17 | run: docker-compose -f docker-compose-test.yml exec -T php_73 rm -rf vendor 18 | - name: Install composer dependencies 19 | run: docker-compose -f docker-compose-test.yml exec -T php_73 composer install 20 | - name: Run PHPUnit Tests 21 | run: docker-compose -f docker-compose-test.yml exec -T php_73 ./vendor/bin/phpunit 22 | test-php-74: 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: actions/checkout@v2 26 | - name: Start the test docker containers 27 | run: docker-compose -f docker-compose-test.yml up -d 28 | - name: Delete vendor 29 | run: docker-compose -f docker-compose-test.yml exec -T php_74 rm -rf vendor 30 | - name: Install composer dependencies 31 | run: docker-compose -f docker-compose-test.yml exec -T php_74 composer install 32 | - name: Run PHPUnit Tests 33 | run: docker-compose -f docker-compose-test.yml exec -T php_74 ./vendor/bin/phpunit 34 | test-php-80: 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v2 38 | - name: Start the test docker containers 39 | run: docker-compose -f docker-compose-test.yml up -d 40 | - name: Delete vendor 41 | run: docker-compose -f docker-compose-test.yml exec -T php_80 rm -rf vendor 42 | - name: Install composer dependencies 43 | run: docker-compose -f docker-compose-test.yml exec -T php_80 composer install 44 | - name: Run PHPUnit Tests 45 | run: docker-compose -f docker-compose-test.yml exec -T php_80 ./vendor/bin/phpunit 46 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Akki Khare 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Hero 3 |

4 | 5 | # Laravel Nova Search 6 | 7 | [![Latest Version](https://img.shields.io/github/release/akki-io/laravel-nova-search.svg?style=flat-square)](https://github.com/akki-io/laravel-nova-search/releases) 8 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 9 | [![StyleCI](https://styleci.io/repos/291209513/shield?branch=master)](https://styleci.io/repos/291209513) 10 | [![Total Downloads](https://img.shields.io/packagist/dt/akki-io/laravel-nova-search.svg?style=flat-square)](https://packagist.org/packages/akki-io/laravel-nova-search) 11 | 12 | ### Testing Statuses 13 | 14 | MySQL - ![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/akki-io/laravel-nova-search/test-mysql.yml?branch=master&style=flat-square) 15 | 16 | SQLite - ![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/akki-io/laravel-nova-search/test-sqlite.yml?branch=master&style=flat-square) 17 | 18 | pgSQL - ![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/akki-io/laravel-nova-search/test-pgsql.yml?branch=master&style=flat-square) 19 | 20 | This package provides a trait that extends the search behaviour of laravel nova resource. 21 | 22 | ## Installation 23 | 24 | You can install the package via composer: 25 | 26 | ```bash 27 | composer require akki-io/laravel-nova-search 28 | ``` 29 | 30 | Next, add `AkkiIo\LaravelNovaSearch\LaravelNovaSearchable` trait to your base resource `App\Nova\Resource` class. 31 | 32 | ```php 33 | use AkkiIo\LaravelNovaSearch\LaravelNovaSearchable; 34 | 35 | abstract class Resource extends NovaResource 36 | { 37 | use LaravelNovaSearchable; 38 | 39 | // ... 40 | } 41 | ``` 42 | 43 | ## Usage 44 | 45 | This package DOES NOT have fuzzy matching capabilities. If you are looking for robust fuzzy matching capabilities provided by "real" search engines, you should look into [Laravel Scout](https://laravel.com/docs/scout). 46 | 47 | This package adds the following types of search to your laravel nova resource. 48 | - Search multiple columns using concatenation. 49 | - Search every word in columns. 50 | - Search relationship columns. 51 | 52 | ### Search multiple columns using concatenation. 53 | 54 | To define which resource fields are searchable, you may assign a two-dimensional array of database columns in the `public static $searchConcatenation` property of your resource class. 55 | Each array in the array are names of columns that are concatenated using whitespace. 56 | 57 | ``` php 58 | /** 59 | * The columns that should be concatenated and searched. 60 | * 61 | * @var array 62 | */ 63 | public static $searchConcatenation = [ 64 | ['first_name', 'last_name'], 65 | ['first_name', 'company'], 66 | ]; 67 | ``` 68 | 69 | ### Search every word in columns. 70 | 71 | To define which resource fields are searchable, you may assign an array of database columns in the `public static $searchMatchingAny` property of your resource class. 72 | Every word in your input is searched for across all these columns. 73 | 74 | ```php 75 | /** 76 | * The columns that should be searched for any matching entry. 77 | * 78 | * @var array 79 | */ 80 | public static $searchMatchingAny = [ 81 | 'first_name', 82 | 'last_name', 83 | 'email', 84 | ]; 85 | ``` 86 | 87 | ### Search relationship columns. 88 | 89 | To define which resource fields are searchable, you may assign an array of database columns in the `public static $searchRelations` property of your resource class. 90 | These database columns are from the related table that is used for searching. 91 | This array has a relationship name as a key, and an array of columns to search for as a value. 92 | 93 | ```php 94 | /** 95 | * The relationship columns that should be searched. 96 | * 97 | * @var array 98 | */ 99 | public static $searchRelations = [ 100 | 'posts' => ['title', 'sub_title'], 101 | ]; 102 | ``` 103 | 104 | #### Nested relationships 105 | 106 | You may search nested relationships using dot notation. 107 | 108 | ```php 109 | /** 110 | * The relationship columns that should be searched. 111 | * 112 | * @var array 113 | */ 114 | public static $searchRelations = [ 115 | 'user.location' => ['state_abbr', 'country_abbr'], 116 | ]; 117 | ``` 118 | 119 | #### Search multiple columns in relationship using concatenation. 120 | 121 | To define which resource fields are searchable, you may assign a two-dimensional array of database columns in the `public static $searchRelationsConcatenation` property of your resource class. 122 | Each array in the array are names of columns that are concatenated using whitespace. 123 | 124 | ``` php 125 | 126 | /** 127 | * The relationship columns that should to be concatenated and searched. 128 | * 129 | * @var array 130 | */ 131 | public static $searchRelationsConcatenation = [ 132 | 'user' => [ 133 | ['first_name', 'last_name'], 134 | ['email'] 135 | ], 136 | ]; 137 | ``` 138 | 139 | #### Search every word in columns of a relationship. 140 | 141 | To define which resource fields are searchable, you may assign an array of database columns in the `public static $searchRelationsMatchingAny` property of your resource class. 142 | Every word in your input is searched for across all these columns. 143 | 144 | ```php 145 | /** 146 | * The relationship columns that should be searched for any matching entry. 147 | * 148 | * @var array 149 | */ 150 | public static $searchRelationsMatchingAny = [ 151 | 'user' => ['first_name', 'last_name'], 152 | ]; 153 | ``` 154 | 155 | ### Testing 156 | 157 | ``` bash 158 | composer test 159 | ``` 160 | 161 | ## Contributing 162 | 163 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 164 | 165 | ### Security 166 | 167 | If you discover any security related issues, please email hello@akki.io instead of using the issue tracker. 168 | 169 | ## Credits 170 | 171 | - [Akki Khare](https://github.com/akki-io) 172 | - [Nova Search Relationship Package](https://github.com/TitasGailius/nova-search-relations) 173 | - [All Contributors](../../contributors) 174 | 175 | ## License 176 | 177 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 178 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "akki-io/laravel-nova-search", 3 | "description": "A package that extends search for laravel nova", 4 | "keywords": [ 5 | "akki-io", 6 | "laravel-nova-search" 7 | ], 8 | "homepage": "https://github.com/akki-io/laravel-nova-search", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Akki Khare", 14 | "email": "hello@akki.io", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^7.3|^8.0" 20 | }, 21 | "require-dev": { 22 | "orchestra/testbench": "^5.4", 23 | "phpunit/phpunit": "^9.0" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "AkkiIo\\LaravelNovaSearch\\": "src" 28 | } 29 | }, 30 | "autoload-dev": { 31 | "psr-4": { 32 | "AkkiIo\\LaravelNovaSearch\\Tests\\": "tests" 33 | } 34 | }, 35 | "scripts": { 36 | "test": "vendor/bin/phpunit" 37 | }, 38 | "config": { 39 | "sort-packages": true 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /docker-compose-test.yml: -------------------------------------------------------------------------------- 1 | version: '2.1' 2 | 3 | services: 4 | php_73: 5 | image: akkica/laravel-base:7.3 6 | depends_on: 7 | db_mysql: 8 | condition: service_healthy 9 | volumes: 10 | - .:/var/www/html 11 | php_74: 12 | image: akkica/laravel-base:7.4 13 | depends_on: 14 | db_mysql: 15 | condition: service_healthy 16 | volumes: 17 | - .:/var/www/html 18 | php_80: 19 | image: akkica/laravel-base:8.0 20 | depends_on: 21 | db_mysql: 22 | condition: service_healthy 23 | volumes: 24 | - .:/var/www/html 25 | db_mysql: 26 | image: mysql 27 | command: --default-authentication-plugin=mysql_native_password 28 | environment: 29 | MYSQL_DATABASE: laravel 30 | MYSQL_USER: laravel 31 | MYSQL_PASSWORD: laravel 32 | MYSQL_ROOT_PASSWORD: laravel 33 | healthcheck: 34 | test: mysql --user=$$MYSQL_USER --password=$$MYSQL_PASSWORD -e 'SHOW DATABASES;' 35 | timeout: 5s 36 | retries: 5 37 | db_pgsql: 38 | image: postgres 39 | environment: 40 | POSTGRES_DB: laravel 41 | POSTGRES_USER: laravel 42 | POSTGRES_PASSWORD: laravel 43 | -------------------------------------------------------------------------------- /hero.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akki-io/laravel-nova-search/bb9649c265f182e8226fac846b7ceaa8e2223c46/hero.png -------------------------------------------------------------------------------- /phpunit.mysql.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | ./src 10 | 11 | 12 | 13 | 14 | ./tests 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /phpunit.pgsql.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | ./src 10 | 11 | 12 | 13 | 14 | ./tests 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/LaravelNovaSearchable.php: -------------------------------------------------------------------------------- 1 | where(function ($query) use ($search) { 88 | parent::applySearch($query, $search); 89 | static::applyConcatenationColumnsSearch($query, $search); 90 | static::applyMatchingAnyColumnsSearch($query, $search); 91 | static::applyRelationSearch($query, $search); 92 | static::applyRelationConcatenationColumnsSearch($query, $search); 93 | static::applyRelationMatchingAnyColumnsSearch($query, $search); 94 | }); 95 | } 96 | 97 | /** 98 | * Apply the concatenation column search query to the given query. 99 | * 100 | * @param \Illuminate\Database\Eloquent\Builder $query 101 | * @param string $search 102 | * @return \Illuminate\Database\Eloquent\Builder 103 | */ 104 | protected static function applyConcatenationColumnsSearch($query, $search) 105 | { 106 | $model = $query->getModel(); 107 | 108 | foreach (static::searchableConcatenationColumns() as $columns) { 109 | if (is_array($columns)) { 110 | $query->orWhereRaw( 111 | static::concatCondition($query, $columns).' '.static::likeOperator($query).' ?', 112 | ['%'.$search.'%'] 113 | ); 114 | } else { 115 | $query->orWhere($model->qualifyColumn($columns), static::likeOperator($query), '%'.$search.'%'); 116 | } 117 | } 118 | 119 | return $query; 120 | } 121 | 122 | /** 123 | * Apply the concatenation column search query to the given query. 124 | * 125 | * @param \Illuminate\Database\Eloquent\Builder $query 126 | * @param string $search 127 | * @return \Illuminate\Database\Eloquent\Builder 128 | */ 129 | protected static function applyMatchingAnyColumnsSearch($query, $search) 130 | { 131 | $model = $query->getModel(); 132 | $tokens = collect(explode(' ', $search)); 133 | 134 | foreach (static::searchableMatchingAnyColumns() as $columns) { 135 | $tokens->each(function ($token) use ($query, $model, $columns) { 136 | $query->orWhere($model->qualifyColumn($columns), static::likeOperator($query), '%'.$token.'%'); 137 | }); 138 | } 139 | 140 | return $query; 141 | } 142 | 143 | /** 144 | * Apply the relationship search query to the given query. 145 | * 146 | * @param \Illuminate\Database\Eloquent\Builder $query 147 | * @param string $search 148 | * @return \Illuminate\Database\Eloquent\Builder 149 | */ 150 | protected static function applyRelationSearch($query, $search) 151 | { 152 | foreach (static::searchableRelationsColumns() as $relation => $columns) { 153 | $query->orWhereHas($relation, function ($query) use ($columns, $search) { 154 | $query->where(static::searchRelationQueryApplier($columns, $search)); 155 | }); 156 | } 157 | 158 | return $query; 159 | } 160 | 161 | /** 162 | * Returns a Closure that applies a search query for a given columns. 163 | * 164 | * @param array $columns 165 | * @param string $search 166 | * @return Closure 167 | */ 168 | protected static function searchRelationQueryApplier(array $columns, string $search) 169 | { 170 | return function ($query) use ($columns, $search) { 171 | $model = $query->getModel(); 172 | $operator = static::likeOperator($query); 173 | 174 | foreach ($columns as $column) { 175 | $query->orWhere($model->qualifyColumn($column), $operator, '%'.$search.'%'); 176 | } 177 | }; 178 | } 179 | 180 | /** 181 | * Apply the relationship column concatenation search query to the given query. 182 | * 183 | * @param \Illuminate\Database\Eloquent\Builder $query 184 | * @param string $search 185 | * @return \Illuminate\Database\Eloquent\Builder 186 | */ 187 | protected static function applyRelationConcatenationColumnsSearch($query, $search) 188 | { 189 | foreach (static::searchableRelationsConcatenationColumns() as $relation => $columns) { 190 | $query->orWhereHas($relation, function ($query) use ($columns, $search) { 191 | $query->where(static::searchRelationConcatenationQueryApplier($columns, $search)); 192 | }); 193 | } 194 | 195 | return $query; 196 | } 197 | 198 | /** 199 | * Returns a Closure that applies a search query for a given concatenated columns. 200 | * 201 | * @param array $columns 202 | * @param string $search 203 | * @return Closure 204 | */ 205 | protected static function searchRelationConcatenationQueryApplier(array $columns, string $search) 206 | { 207 | return function ($query) use ($columns, $search) { 208 | $model = $query->getModel(); 209 | $operator = static::likeOperator($query); 210 | 211 | foreach ($columns as $items) { 212 | if (is_array($items)) { 213 | $query->orWhereRaw( 214 | static::concatCondition($query, $items).' '.$operator.' ?', 215 | ['%'.$search.'%'] 216 | ); 217 | } else { 218 | $query->orWhere($model->qualifyColumn($items), $operator, '%'.$search.'%'); 219 | } 220 | } 221 | }; 222 | } 223 | 224 | /** 225 | * Apply the relationship matching any column search query to the given query. 226 | * 227 | * @param \Illuminate\Database\Eloquent\Builder $query 228 | * @param string $search 229 | * @return \Illuminate\Database\Eloquent\Builder 230 | */ 231 | protected static function applyRelationMatchingAnyColumnsSearch($query, $search) 232 | { 233 | foreach (static::searchableRelationsMatchingAnyColumns() as $relation => $columns) { 234 | $query->orWhereHas($relation, function ($query) use ($columns, $search) { 235 | $query->where(static::searchRelationMatchingAnyQueryApplier($columns, $search)); 236 | }); 237 | } 238 | 239 | return $query; 240 | } 241 | 242 | /** 243 | * Returns a Closure that applies a matching any search query for a given columns. 244 | * 245 | * @param array $columns 246 | * @param string $search 247 | * @return Closure 248 | */ 249 | protected static function searchRelationMatchingAnyQueryApplier(array $columns, string $search) 250 | { 251 | return function ($query) use ($columns, $search) { 252 | $model = $query->getModel(); 253 | $operator = static::likeOperator($query); 254 | $tokens = collect(explode(' ', $search)); 255 | 256 | foreach ($columns as $items) { 257 | $tokens->each(function ($token) use ($query, $model, $items, $operator) { 258 | $query->orWhere($model->qualifyColumn($items), $operator, '%'.$token.'%'); 259 | }); 260 | } 261 | }; 262 | } 263 | 264 | /** 265 | * Resolve the query operator. 266 | * 267 | * @param \Illuminate\Database\Eloquent\Builder $query 268 | * @return string 269 | */ 270 | protected static function likeOperator($query) 271 | { 272 | if ($query->getModel()->getConnection()->getDriverName() === 'pgsql') { 273 | return 'ILIKE'; 274 | } 275 | 276 | return 'LIKE'; 277 | } 278 | 279 | /** 280 | * Resolve the concat condition. 281 | * 282 | * @param $query 283 | * @param $columns 284 | * @return string 285 | */ 286 | protected static function concatCondition($query, $columns) 287 | { 288 | if ($query->getModel()->getConnection()->getDriverName() === 'sqlite') { 289 | return implode(" || ' ' || ", $columns); 290 | } 291 | 292 | // Concat with COALESCE to turn possible NULL values into empty strings. 293 | foreach ($columns as $idx => $column) { 294 | $columns[$idx] = sprintf("COALESCE(%s, '')", $column); 295 | } 296 | 297 | // Replace double empty spaces created by an empty COALESCE concat. 298 | return 'REPLACE(CONCAT('.implode(", ' ', ", $columns)."), ' ', ' ') "; 299 | } 300 | } 301 | --------------------------------------------------------------------------------