【问题标题】:Laravel seed database with special character in string字符串中具有特殊字符的 Laravel 种子数据库
【发布时间】:2014-12-18 21:24:03
【问题描述】:

在 Laravel 中,我试图在我的数据库中植入一些可能包含特殊字符的文本字段。当我执行db:seed 时,它说它成功地为数据库播种,但是当我查看我的数据库时,我看到它插入了除特殊字符之后的文本之外的所有内容(在该字段中)。

这是我的种子的样子:

City::create(array(
  'name' => 'Groningen',
  'description' => 'A simple description with a special character é in it, and
   some content that for some reason won\'t be inserted to my database'
));

所以要明确一点:'é' 之前的文本被插入到数据库中,但它之后的文本不是。所以我的数据库看起来像:

id   |  name           |  description
-----|-----------------|-------------------
1    |  Groningen      |  A simple description with a special character 

我的城市模型

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class City extends Model {


  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'cities';

}

我的城市迁移文件:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCitiesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cities', function($t) {
            $t->increments('id');

            $t->string('name');
            $t->text('description');

            $t->double('latitude')->nullable();
            $t->double('longitude')->nullable();

            $t->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('cities');
    }

}

还有我的 config/database.php:

return [

    'default' => 'mysql',
    'connections' => [

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'citydb',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ],
    ],
    // The other Laravel default database.php config

【问题讨论】:

    标签: php database laravel model eloquent


    【解决方案1】:

    您的描述字段似乎有字符限制。你应该确保它足够长。

    但如果不是这样,您应该确保使用正确的编码对文件进行编码,使用正确的编码创建数据库,并且与您在app/config/database.php(或app/config/local/database.php)中设置的相同。

    例如,您可以在此文件中设置:

    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    

    并确保您的数据库(和表列设置为utf8_unicode_ci,并且您放置字符串的文件编码为UTF-8

    最后一个选项是您的City Eloquent 模型。你应该确保你没有在这里使用任何修改器(例如str_replacesubstr 或类似的函数,它们可能会修剪数据或以错误的方式修改数据 - 例如使用substr 而不是mb_substr

    【讨论】:

    • - 字段限制不是问题(当我删除它起作用的特殊字符时)- 我已经像这样设置了我的数据库配置- 除了返回我的数据之外我什么也没做模型,(仅从数据库返回不正确的数据)
    • @DanielGelling 你的City 模型有什么功能吗?它是继承自 Eloquent 还是继承自第 3 方插件?
    • @MarnickNabialek 不,我没有。它只包含一个受保护的 $table;
    • @DanielGelling 所以可能你可以附上你的文件:City 模型、迁移(如果你有的话)或者你的数据库表、你的数据库配置文件和你用于播种的文件
    • @DanielGelling 当我保存带有种子而不是UTF-8 但例如ISO-8859-1 的文件时,我已经重现了该错误。确保你有这个文件是 UTF-8 编码,而不是我在回答中提到的任何其他编码。
    猜你喜欢
    • 2021-01-21
    • 2019-09-03
    • 1970-01-01
    • 2011-04-11
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2019-09-10
    相关资源
    最近更新 更多