laravel-mysql-use-index-scope icon indicating copy to clipboard operation
laravel-mysql-use-index-scope copied to clipboard

use with DB::

Open omidMolaverdi opened this issue 4 years ago • 1 comments

Thanks for the great work.

Is there any way to change this package to work with the DB:: query builder? for example the query below: DB::select("select name from users USE INDEX (names) limit 10");

being written like: DB::table('users')->select('name')->useIndex('names')->limit(10);

omidMolaverdi avatar Nov 21 '21 14:11 omidMolaverdi

@omidMolaverdi put this in your AppServiceProvider to use it anywhere 👍

Builder::macro('useIndex', function (string $indexName) {
    $tableName = $this->getQuery()->from;
    $this->getQuery()->from(\DB::raw("{$tableName} USE INDEX ({$indexName})"));

    return $this;
});

tylerwiegand avatar Jun 03 '22 23:06 tylerwiegand