是否可以从laravel迁移中更改mysql数据库编码字符集? [英] Is it possible to change mysql DB encoding charset from within a laravel migration?
问题描述
是否可以通过迁移执行此操作?或者我必须写一个单独的脚本?
迁移(为了完整)
/ **
*运行迁移。
*
* @return void
* /
public function up()
{
DB :: raw('ALTER TABLE homeestead.survey_responses CONVERT TO CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci');
DB :: raw('REPAIR TABLE homeestead.survey_responses');
DB :: raw('OPTIMIZE TABLE homestead.survey_responses');
}
/ **
*反转迁移。
*
* @return void
* /
public function down()
{
DB :: raw('ALTER TABLE homestead.survey_responses CONVERT TO CHARACTER SET = utf8 COLLATE utf8_unicode_ci');
DB :: raw('REPAIR TABLE homeestead.survey_responses');
DB :: raw('OPTIMIZE TABLE homestead.survey_responses');
}
上述迁移运行没有错误,但不幸的是什么都不做。 >
使用通常用于迁移类的 Schema / Blueprint 类是不可能的。但是,您仍然可以使用 DB 外观来运行任何需要使用的SQL:
使用照明\Database\Migrations\Migration;
使用Illuminate\Support\Facades\DB;
class ChangeCharsetAndCollation Migration
{
public function up()
{
DB :: statement('ALTER DATABASE数据库CHARACTER SET new_charset COLLATE new_collation') ;
}
public function down()
{
DB :: statement('ALTER DATABASE数据库CHARACTER SET old_charset COLLATE old_collation');
}
}
只需替换数据库, old_charset ,旧约会, new_charset , new_collation ,并确保您的用户具有运行此查询的权限。
I'm attempting to change the encoding of a mysql DB in Laravel-5, I've tried with a migration, following this example: https://slashdot.io/blog/adding-emoji-support-to-your-blog-948181198 - However, nothing ever updates and the charset / encoding stays as it was.
Is it possible to do this with a migration? Or will I have to write a separate script?
migration (for completeness)
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::raw('ALTER TABLE homestead.survey_responses CONVERT TO CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci');
DB::raw('REPAIR TABLE homestead.survey_responses');
DB::raw('OPTIMIZE TABLE homestead.survey_responses');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::raw('ALTER TABLE homestead.survey_responses CONVERT TO CHARACTER SET = utf8 COLLATE utf8_unicode_ci');
DB::raw('REPAIR TABLE homestead.survey_responses');
DB::raw('OPTIMIZE TABLE homestead.survey_responses');
}
The above migration runs, without errors, but unfortunately does nothing.
It's not possible using Schema/Blueprint classes that are usually used in migration classes. But you can still run any SQL you need using DB facade - in your case:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
class ChangeCharsetAndCollation Migration
{
public function up()
{
DB::statement('ALTER DATABASE database CHARACTER SET new_charset COLLATE new_collation');
}
public function down()
{
DB::statement('ALTER DATABASE database CHARACTER SET old_charset COLLATE old_collation');
}
}
Just replace database, old_charset, old_collation, new_charset, new_collation with desired values and make sure your user has permissions to run such query.
这篇关于是否可以从laravel迁移中更改mysql数据库编码字符集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!