查询在Laravel中添加全文索引

时间:2016-03-23 09:35:08

标签: php mysql laravel full-text-indexing laravel-query-builder

ALTER TABLE TableName     添加FULLTEXT INDEX IndexNameColumnName);

这是在mysql中添加全文索引的查询。

但是,我想要添加全文索引的laravel查询。

提前感谢......

3 个答案:

答案 0 :(得分:4)

应该这样做:DB::statement

DB::statement('ALTER TABLE TableName ADD FULLTEXT INDEX IndexName (ColumnName)');

答案 1 :(得分:2)

在Laravel> = 6.15.0上,您可以像这样扩展Illuminate\Database\Schema\Grammars\MySqlGrammarIlluminate\Database\Schema\Blueprint类(例如,在boot()的{​​{1}}方法上):

AppServiceProvider

并在您的迁移中像这样使用它:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
use Illuminate\Support\Fluent;

Blueprint::macro('fulltext', function ($columns, $name = null, $algorithm = null)
{
    return $this->indexCommand('fulltext', $columns, $name, $algorithm);
});

Blueprint::macro('dropFulltext', function ($index)
{
    return $this->dropIndexCommand('dropIndex', 'fulltext', $index);
});

MySqlGrammar::macro('compileFulltext', function (Blueprint $blueprint, Fluent $command)
{
    return $this->compileKey($blueprint, $command, 'fulltext');
});

答案 2 :(得分:0)

对于多个Columns/Fields

DB::statement('ALTER TABLE Database.TableName ADD FULLTEXT fulltext_index (Col_1, col_2, col_3)');