Skip to content

Commit 56a4867

Browse files
committed
added method forceIndex
1 parent a13867a commit 56a4867

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ I often use this in my work and I hope it will be useful to you!
1414

1515
## Last added
1616

17+
2021/01/24 - Added method [forceIndex](https://github.com/Alexmg86/laravel-sub-query/wiki/Some-sugar#force-index)
1718
2020/12/10 - Added casting for withSum('items:price:signed') and others
1819
2020/11/03 - Added method [withMath](https://github.com/Alexmg86/laravel-sub-query#working-with-columns)
1920
2020/10/21 - Added [some sugar](https://github.com/Alexmg86/laravel-sub-query#sugar)

src/Traits/LaravelSubQuerySugar.php

+10
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,14 @@ public function withMath($columns, $operator = '+', $name = null)
9191

9292
return $this->addSelect(new Expression("$query as $asName"));
9393
}
94+
95+
/**
96+
* Force index by column
97+
* @param string $column
98+
* @return $this
99+
*/
100+
public function forceIndex($column)
101+
{
102+
return $this->query->from(\DB::raw($this->query->from . ' FORCE INDEX (' . $column . ')'));
103+
}
94104
}

tests/DatabaseTestCase.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ protected function createGoodsTable(): void
6363
protected function createCountriesTable(): void
6464
{
6565
Schema::create('countries', function (Blueprint $table) {
66-
$table->id();
66+
$table->increments('id');
6767
$table->string('name');
6868
});
6969
}
7070

7171
protected function createCustomersTable(): void
7272
{
7373
Schema::create('customers', function (Blueprint $table) {
74-
$table->id();
74+
$table->increments('id');
7575
$table->integer('country_id');
7676
$table->string('name');
7777
});
@@ -80,7 +80,7 @@ protected function createCustomersTable(): void
8080
protected function createPostsTable(): void
8181
{
8282
Schema::create('posts', function (Blueprint $table) {
83-
$table->id();
83+
$table->increments('id');
8484
$table->integer('user_id');
8585
$table->string('title');
8686
});

tests/LaravelSubQuerySugarTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,13 @@ public function testWithMaths()
8686

8787
$this->assertEquals(10, $results->multi);
8888
}
89+
90+
public function testForceIndex()
91+
{
92+
$this->createBasic();
93+
94+
$result = Invoice::forceIndex('name')->toSql();
95+
96+
$this->assertEquals('select * from invoices FORCE INDEX (name)', $result);
97+
}
8998
}

0 commit comments

Comments
 (0)