vpominchuk / laravel-mysql-use-index-scope by vpominchuk

A super simple package allowing for use MySQL `USE INDEX` and `FORCE INDEX` statements
303,719
38
1
Package Data
Maintainer Username: vpominchuk
Maintainer Contact: vpominchuk@gmail.com (Vasyl Pominchuk)
Package Create Date: 2021-09-12
Package Last Update: 2024-04-19
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-21 03:00:07
Package Statistics
Total Downloads: 303,719
Monthly Downloads: 11,520
Daily Downloads: 43
Total Stars: 38
Total Watchers: 1
Total Forks: 5
Total Open Issues: 1

Latest Version on Packagist Software License Total Downloads

Laravel MySQL Use Index Scope

A super simple package allowing for use MySQL USE INDEX and FORCE INDEX statements.

Requirements

  • PHP ^7.4 | ^8.0
  • Laravel 6, 7, 8 and 9

Installation

composer require vpominchuk/laravel-mysql-use-index-scope

Usage

Simply reference the required trait in your model:

Model:

    use VPominchuk\ModelUseIndex;
    
    class MyModel extends Model
    {
        use ModelUseIndex;
    }

Anywhere in the code:

    $builder = MyModel::where('name', $name)->where('age', $age)->
        useIndex($indexName)->...

Database table structure:

You need to create a named index with required name. For example:

Laravel Migration:

    $table->index(['name', 'age'], 'user_age_index');

Available methods

useIndex($indexName)

Tells MySQL to use an index if it possible.

forceIndex($indexName)

Force MySQL to use an index if it possible.

ignoreIndex($indexName)

Ask MySQL to ignore an index if it possible.

Security

If you discover any security related issues, please use the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.