├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src └── ModelUseIndex.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /.idea 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 F9 Web Ltd. 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://banners.beyondco.de/Laravel%20MySQL%20USE%20INDEX%20Model%20Scope.png?theme=light&packageManager=composer+require&packageName=vpominchuk%2Flaravel-mysql-use-index-scope&pattern=texture&style=style_1&description=Allowing+for+use+MySQL+USE+INDEX+and+FORCE+INDEX+statements&md=1&showWatermark=0&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg) 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/vpominchuk/laravel-mysql-use-index-scope.svg?style=flat-square)](https://packagist.org/packages/vpominchuk/laravel-mysql-use-index-scope) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/vpominchuk/laravel-mysql-use-index-scope.svg?style=flat-square)](https://packagist.org/packages/vpominchuk/laravel-mysql-use-index-scope) 6 | 7 | # Laravel MySQL Use Index Scope 8 | A super simple package allowing for use MySQL `USE INDEX` and `FORCE INDEX` statements. 9 | 10 | ## Requirements 11 | - PHP `^7.4 | ^8.0` 12 | - Laravel 6, 7, 8, 9, 10, and 11 13 | 14 | ## Installation 15 | 16 | `composer require vpominchuk/laravel-mysql-use-index-scope` 17 | 18 | ## Usage 19 | Simply reference the required trait in your model: 20 | 21 | ### Model: 22 | ```php 23 | use VPominchuk\ModelUseIndex; 24 | 25 | class MyModel extends Model 26 | { 27 | use ModelUseIndex; 28 | } 29 | ``` 30 | 31 | ### Anywhere in the code: 32 | ```php 33 | $builder = MyModel::where('name', $name)->where('age', $age)-> 34 | useIndex($indexName)->... 35 | ``` 36 | 37 | ### Database table structure: 38 | You need to create a named index with required name. For example: 39 | 40 | Laravel Migration: 41 | ```php 42 | $table->index(['name', 'age'], 'user_age_index'); 43 | ``` 44 | ## Available methods 45 | #### `useIndex($indexName)` 46 | Tells MySQL to use an index if it possible. 47 | 48 | #### `forceIndex($indexName)` 49 | Force MySQL to use an index if it possible. 50 | 51 | #### `ignoreIndex($indexName)` 52 | Ask MySQL to ignore an index if it possible. 53 | 54 | ## Security 55 | 56 | If you discover any security related issues, please use the issue tracker. 57 | 58 | ## Credits 59 | 60 | - [Vasyl Pominchuk](https://github.com/vpominchuk) 61 | 62 | ## License 63 | 64 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 65 | 66 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vpominchuk/laravel-mysql-use-index-scope", 3 | "description": "A super simple package allowing for use MySQL `USE INDEX` and `FORCE INDEX` statements", 4 | "keywords": [ 5 | "laravel", 6 | "laravel mysql", 7 | "laravel mysql use index", 8 | "laravel mysql force index" 9 | ], 10 | "homepage": "https://github.com/vpominchuk/laravel-mysql-use-index-scope", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Vasyl Pominchuk", 15 | "email": "vpominchuk@gmail.com", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php": "^7.4|^8.0", 21 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "VPominchuk\\": "src" 26 | } 27 | }, 28 | "config": { 29 | "sort-packages": true 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/ModelUseIndex.php: -------------------------------------------------------------------------------- 1 | getTable(); 40 | $index = $this->parseIndexName($index); 41 | 42 | $this->from[] = "USE INDEX($index)"; 43 | 44 | $raw = "`$table` " . implode(" ", $this->from); 45 | 46 | return $query->from(DB::raw($raw)); 47 | } 48 | 49 | /** 50 | * @param $query 51 | * @param string|array $index 52 | * @return Builder 53 | */ 54 | public function scopeForceIndex($query, $index): Builder 55 | { 56 | $table = $this->getTable(); 57 | $index = $this->parseIndexName($index); 58 | 59 | $this->from[] = "FORCE INDEX($index)"; 60 | 61 | $raw = "`$table` " . implode(" ", $this->from); 62 | 63 | return $query->from(DB::raw($raw)); 64 | } 65 | 66 | /** 67 | * @param $query 68 | * @param string|array $index 69 | * @return Builder 70 | */ 71 | public function scopeIgnoreIndex($query, $index): Builder 72 | { 73 | $table = $this->getTable(); 74 | $index = $this->parseIndexName($index); 75 | 76 | $this->from[] = "IGNORE INDEX($index)"; 77 | 78 | $raw = "`$table` " . implode(" ", $this->from); 79 | 80 | return $query->from(DB::raw($raw)); 81 | } 82 | } 83 | --------------------------------------------------------------------------------