我正在使用Laravel 5.3,我已经设置了我的生产服务器.所有数据库迁移都已使用以下数据库配置创建:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

But now, some of my users had reported that they get an error when they try to save a form that has emoji ? icons in them. After searching I found out that I need to set the mysql charset to utf8mb4 for this to work so my config should have been something like this instead:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

因为这是在生产服务器中,所以我不能执行migrate:refresh.所以我的问题是:

  1. 如何将使用laravel迁移创建的现有数据库更改为使用utf8mb4而不是utf8,并在相同的基础上更新laravel?有更简单的方法吗?
  2. If the above is possible, am I better off setting utf8mb4 for all tables or only use that for the 2 table columns where I will really be using emoji.

谢谢你的帮助.

推荐答案

  1. 使用原始mysql查询编写更新表迁移脚本并运行php artisan migrate命令

     use Illuminate\Database\Migrations\Migration;
    
     class UpdateTableCharset extends Migration {
    
         /**
          * Run the migrations.
          *
          * @return void
          */
         public function up() {
                 DB::unprepared('ALTER TABLE `table_name` CONVERT TO CHARACTER SET utf8mb4');
         }
    
         /**
          * Reverse the migrations.
          *
          * @return void
          */
         public function down() {
                 DB::unprepared('ALTER TABLE `table_name` CONVERT TO CHARACTER SET utf8');
         }
     }
    
  2. 我个人喜好,更新餐桌.我没有证据可以说它更好

注意:您仍然需要将数据库配置保持为utf8mb4.

Laravel相关问答推荐

如何判断用户是否已登录Laravel

Laravel Livewire与自定义JS有关的多姆问题

RuntimeException 未安装 Zip PHP 扩展

如何在 vue 模板中引用 laravel csrf 字段

使用 Amazon S3 的 Storage::get() 返回 false

Laravel 附加额外字段

Laravel 5.2 中的正则表达式验证

artisan 迁移错误找不到类'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver'

如何在同一行显示 Laravel artisan 命令输出?

Laravel Electron邮件验证模板位置

Laravel 5:完整性约束违规:1452 无法添加或更新子行:外键约束失败

Laravel 同步错误

Laravel 4 - 如何将所有字段的所有验证错误消息作为 JSON struct 返回?

Laravel - 语法错误,文件意外结束

在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

如何在 Laravel 或 Redis 中取消排队的作业(job)

Laravel 5.4 Vue.JS 无法挂载组件:未定义模板或渲染函数

在我的编辑器中管理上传文件的最佳方式?

如何判断 Laravel 集合是否为空?

Laravel:如何在 PhpUnit 上启用堆栈跟踪错误