1

我正在学习 laravel,我被困在一个简单的过程中。我希望将表生成为 UTF-8,但 varchar 和文本字段就像 latin-1。

指南中的架构部分根本没有帮助。我找到了这个 GitHub 条目 ,但它也不起作用(向我抛出错误)。

我有这样的架构:

<?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'   => '',
),

我错过了什么,我该如何解决?

谢谢,

4

5 回答 5

2

支持MySQL character setscollation已添加到Laravel 4. 检查这个例子。

https://github.com/laravel/laravel/pull/897

于 2014-08-19T12:23:54.190 回答
1

对于 Laravel 3 master 中的单个架构构建,似乎没有实际实现。(在源代码中找不到任何东西)

我认为您需要手动执行此操作(尝试使用原始查询

于 2013-01-29T15:26:34.403 回答
1

我坚持同样的做法,我想 Laravel 无法更改/设置字符集的排序规则。但是您可以在您my.ini/my.cnf的 MySQL 服务器中更改 charset 的默认排序规则!

[mysqld]
#...
collation-server = utf8_unicode_ci
#...
于 2013-03-05T14:24:16.317 回答
1

刚刚解决了它,(使用 mysql 5.5 (5.5.29))和 Linux Mint 13 Mate(基于 Ubuntu 12.04 LTS)

我在 /etc/mysqld/my.cnf 添加了这些行

[client]
default-character-set = utf8

[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
character-set-server = utf8
collation-server = utf8_unicode_ci

这样,SQL 查询在 shell 上正常工作,但在 laravel 或 phpmyadmin 等上,它们没有工作。所以我这样做了:

我打开 /etc/apache2/conf.d/charset 并删除了 ; 从这一行:

AddDefaultCharset UTF-8

然后我重新启动了 apache2 和 mysql,现在它可以正常工作了。

稍后编辑:GitHub Core Hack就像一种享受。我最终做到了。

于 2013-03-06T15:03:01.770 回答
0

这是我的解决方案

1.在laravel public $charset;/database/schema/table.php 下public $engine; 添加 2.替换 laravel/database/schema/grammars/mysql.php 中的函数 create 像这样

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.现在您可以在迁移 php 中设置任何字符集,例如$table->charset='utf8';

于 2013-08-18T07:12:52.837 回答