Laravel 3 Schema Table Column Collation

前端 未结 5 2101
耶瑟儿~
耶瑟儿~ 2021-02-06 19:44

I\'m learning laravel, and I\'m stuck on a simple process. I want the tables to be generated as UTF-8 but varchar and text fields are like latin-1.

Schema section in gui

5条回答
  •  梦谈多话
    2021-02-06 20:18

    Here is my solution

    1.add public $charset; in the laravel/database/schema/table.php under public $engine; 2.replace the function create in laravel/database/schema/grammars/mysql.php like this

    public function create(Table $table, Fluent $command)
    {
        $columns = implode(', ', $this->columns($table));
    
        // First we will generate the base table creation statement. Other than auto
        // incrementing keys, no indexes will be created during the first creation
        // of the table as they're added in separate commands.
        $sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
    
        if ( ! is_null($table->engine))
        {
            $sql .= ' ENGINE = '.$table->engine;
        }
        if ( ! is_null($table->charset))
        {
            $sql.="DEFAULT CHARSET=".$table->charset;
        }
        return $sql;
    }
    

    3.now you can set any charset in your migration php such as $table->charset='utf8';

提交回复
热议问题