我正在制作一个Laravel软件包,主要用于内部项目,但我遇到了一些小麻烦.
我在每个需要搜索的模型中添加一个searchable全文列。我在使用Laravel的本地迁移特性,比如-
Schema::table('assets', function (Blueprint $table) {
Searchable::migrateUp($table, 'moderated');
});因此,这将调用一个方法来部署moderated列之后的迁移。这个方法是什么样子的-
public function migrateUp(Blueprint $table, $after = null)
{
// add our searchable column
$table
->longText('searchable')
->after($after)->nullable();
// ToDo: get indexing working on migration
// add a fulltext index
DB::statement(
'ALTER TABLE ? ADD FULLTEXT fulltext_searchable (?)',
[$table->getTable(), 'searchable']
);
// return our table
return $table;
}因此,创建了一个可为空的长文本字段,然后我尝试从该字段创建一个完整的文本索引。问题是,在我运行我的语句时,可搜索的列实际上还不存在。在迁移文件中调用Searchable::migrateUp()的用户仍然很简单的时候,我有没有办法做到这一点呢?
感谢你的指点!克里斯。
发布于 2018-09-03 12:49:36
我认为当我想出这个问题时,我的代码是盲目的--你知道吗,当你看不到简单的解决方案过于复杂的时候?!感谢@JonasStaudenmeir,我的解决方案如下-
我的迁徙-
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Searchable::migrateUp('assets', 'moderated');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Searchable::migrateDown('assets');
}我的方法-
/**
* Creates searchable column on model table
*
* @param string $table Table name to perform migration on
* @param string|null $after Whether to add after a particular column
*
* @return void
*/
public function migrateUp($table, $after = null)
{
// add to our schema
Schema::table(
$table,
function (Blueprint $table) use ($after) {
$table->longText($this->_searchableColumnKey)
->after($after)->nullable();
}
);
// create our index
\DB::statement("ALTER TABLE {$table} ADD FULLTEXT fulltext_searchable ({$this->_searchableColumnKey})");
}
/**
* Removes searchable column on table
*
* @param Blueprint $table Requires blueprint
*
* @return void
*/
public function migrateDown($table)
{
Schema::table(
$table,
function (Blueprint $table) {
$table->dropColumn($this->_searchableColumnKey);
}
);
}因此,与其由Schema调用我的方法,不如从方法内部运行模式!
https://stackoverflow.com/questions/52138974
复制相似问题