是否可以从laravel迁移中更改mysql数据库编码字符集? [英] Is it possible to change mysql DB encoding charset from within a laravel migration?

查看:238
本文介绍了是否可以从laravel迁移中更改mysql数据库编码字符集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改Laravel-5中的一个mysql数据库的编码,我已经尝试过迁移,按照这个例子: https://slashdot.io/blog/adding-emoji-support-to-your-blog-948181198 - 但是,没有任何更新并且字符集/编码保持原样。



是否可以通过迁移执行此操作?或者我必须写一个单独的脚本?



迁移(为了完整)

  / ** 
*运行迁移。
*
* @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 ChangeCharsetAndCollat​​ion Migration
{
public function up()
{
DB :: statement('ALTER DATABASE数据库CHARACTER SET new_charset COLLATE new_collat​​ion') ;
}

public function down()
{
DB :: statement('ALTER DATABASE数据库CHARACTER SET old_charset COLLATE old_collat​​ion');
}
}

只需替换数据库 old_charset 旧约会 new_charset new_collat​​ion ,并确保您的用户具有运行此查询的权限。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆