In my case I added made an overwrite for the string-method like this:
/**
* {@inheritDoc}
*
* Overwrites the string function to add a way to define a charset
* It creates an anonym class to overwrite buildCompleteString()
* and append the charset after the '{length}' placeholder
*
* @see ColumnSchemaBuilder::buildCompleteString()
*/
public function string($length = null, $charset = null) {
$type = new class(Schema::TYPE_STRING, $length, $this->getDb()) extends ColumnSchemaBuilder {
public $charset = null;
function buildCompleteString($format) {
$charsetSql = $this->charset !== null ? ' CHARACTER SET ' . $this->charset: '';
return parent::buildCompleteString(
strtr($format, ['{length}' => "{length}{$charsetSql}"])
);
}
};
$type->charset = $charset;
return $type;
}
This method creates an anonym class to modify format in the buildCompleteString()
method of the SchemaBuilder.
Because charset and colation must be after the col type/length, I replace the {lenght}
placeholder and add the charset after this.
The good point is, when you once have this method (e.g. in your own migration class which inherits from the basic migration class) you can use this method on each col to specify even different charsets for each col.
E.g. like this
$this->createTable('some_table', [
'column_1' => $this->string(64, 'utf8 COLLATE utf8_unicode_ci')->notNull(),
'column_2' => $this->integer()->notNull(),
'column_3' => $this->integer(),
'column_4' => $this->string(64, 'binary')->notNull(),
]);
The only possible downside of this could be, when you try to use this code for multiple database types that maybe have a different syntax for defining charsets and collation.