这是我当前的迁移:
class News extends Migration
{
public function up()
{
Schema::create('News', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('description');
$table->integer('user_id')->unsigned()->index();
$table->string('imgPath')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::drop('News');
}
}
现在我需要分别对这些列进行全文索引:title
description
。所以我正在寻找这样的东西:->fulltext()
。但是我在 Laravel 文档中找不到任何类似的东西。
无论如何,
- 如何在迁移中对单个列建立全文索引?喜欢:
(标题)
- 另请注意,如何在迁移中对多个列创建复合全文索引?喜欢:
(标题,描述)
注意:我想要一个可以让我像这样搜索的 intex:。 . .匹配(col)对('值')
最佳答案
Laravel 不支持全文搜索。 但是您可以将原始查询用作:
DB::statement('ALTER TABLE News ADD FULLTEXT search(title, description)');
注意 - 如果您不使用 MySQL 5.6+,我们必须将数据库引擎设置为 MyISAM 而不是 InnoDB。
$table->engine = 'MyISAM'; // means you can't use foreign key constraints
对于搜索,你可以这样做:
$q = Input::get('query');
->whereRaw("MATCH(title,description) AGAINST(? IN BOOLEAN MODE)", array($q))
关于php - 如何制作列的全文索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40316234/