Skip to content

Commit 650bdd1

Browse files
committed
Initial Revision
1 parent 7fb4b35 commit 650bdd1

File tree

5 files changed

+121
-9
lines changed

5 files changed

+121
-9
lines changed

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
composer.phar
2-
/vendor/
3-
4-
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
5-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock
1+
/vendor
2+
/.idea

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Vasyl
3+
Copyright (c) 2021 F9 Web Ltd.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,57 @@
1-
# laravel-mysql-use-index-scope
2-
A super simple package allowing for use MySQL 'USE INDEX' and 'FORCE INDEX' statements.
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+
# Laravel MySQL Use Index Scope
3+
A super simple package allowing for use MySQL `USE INDEX` and `FORCE INDEX` statements.
4+
5+
## Requirements
6+
- PHP `^7.4 | ^8.0`
7+
- Laravel 6, 7 and 8
8+
9+
## Installation
10+
11+
`composer require vpominchuk/laravel-mysql-use-index-scope`
12+
13+
## Usage
14+
Simply reference the required trait in your model:
15+
16+
### Model:
17+
```php
18+
use VPominchuk\ModelUseIndex;
19+
20+
class MyModel extends Model
21+
{
22+
use ModelUseIndex;
23+
}
24+
```
25+
26+
### Anywhere in the code:
27+
```php
28+
$builder = MyModel::where('name', $name)->where('age', $age)->
29+
useIndex($indexName)->...
30+
```
31+
32+
### Database table structure:
33+
You need to create a named index with required name. For example:
34+
35+
Laravel Migration:
36+
```php
37+
$table->index(['name', 'age'], 'user_age_index');
38+
```
39+
## Available methods
40+
#### `useIndex($indexName)`
41+
Tells MySQL to use an index if it possible.
42+
43+
#### `forceIndex($indexName)`
44+
Force MySQL to use an index if it possible.
45+
46+
## Security
47+
48+
If you discover any security related issues, please use the issue tracker.
49+
50+
## Credits
51+
52+
- [Vasyl Pominchuk](https://github.com/vpominchuk)
53+
54+
## License
55+
56+
The MIT License (MIT). Please see [License File](LICENSE) for more information.
57+

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
"homepage": "https://pominchuk.com",
17+
"role": "Developer"
18+
}
19+
],
20+
"require": {
21+
"php": "^7.4 | ^8.0",
22+
"illuminate/support": "^6.0|^7.0|^8.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"VPominchuk\\": "src"
27+
}
28+
},
29+
"config": {
30+
"sort-packages": true
31+
},
32+
"minimum-stability": "dev",
33+
"prefer-stable": true
34+
}

src/ModelUseIndex.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace VPominchuk;
4+
5+
6+
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Support\Facades\DB;
8+
9+
/**
10+
* @method Builder useIndex(string $index)
11+
* @method Builder forceIndex(string $index)
12+
*
13+
*/
14+
trait ModelUseIndex
15+
{
16+
public function scopeUseIndex($query, string $index): Builder
17+
{
18+
$table = $this->getTable();
19+
return $query->from(DB::raw("`$table` USE INDEX(`$index`)"));
20+
}
21+
22+
public function scopeForceIndex($query, string $index): Builder
23+
{
24+
$table = $this->getTable();
25+
return $query->from(DB::raw("`$table` FORCE INDEX(`$index`)"));
26+
}
27+
}

0 commit comments

Comments
 (0)