【发布时间】:2013-01-13 03:47:16
【问题描述】:
我正在学习 laravel,但我被困在一个简单的过程中。我希望将表格生成为 UTF-8,但 varchar 和文本字段就像 latin-1。
Schema section in guide 根本没有帮助。我找到了this GitHub entry,但它也不起作用(抛出错误)。
我有这样的架构:
<?php
class Create_Authors_Table {
/**
* Make changes to the database.
*
* @return void
*/
public function up()
{
Schema::create('authors',function($table){
//$table->charset('utf8'); //does not work
//$table->collate('utf8_general_ci'); //does not work
$table->increments('id');
$table->string('name')->charset('utf8'); //adding ->collate('utf8_general_ci') does not work
$table->text('bio')->charset('utf8');
$table->timestamps();
});
}
/**
* Revert the changes to the database.
*
* @return void
*/
public function down()
{
Schema::drop('authors');
}
}
这是 SQL 输出:
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`bio` text NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
但这正是我需要的:
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`bio` text NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
我什至把 collation 键放到我的 application/config/database.php 中
'mysql' => array(
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation'=> 'utf8_unicode_ci', //this line was not there out of the box, googling provided me this
'prefix' => '',
),
我错过了什么,我该如何解决?
谢谢,
【问题讨论】:
标签: database database-schema laravel database-migration laravel-3