drzyeetvt41077335 2016-09-06 23:19
浏览 230

Laravel ORM数组到字符串转换

Laravel 5.3, PHP 5.6 Fresh laravel new project, minimal configuration.

I have made a simple migration and model, and am trying to seed data into it via php artisan tinker.

My migration looks like this:

<?php

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

class CreatesFooTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('foo', function (Blueprint $table) {
            $table->increments('id');
            $table->string('foo', 20);
            $table->timestamps();
        });
    }

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

When I run php artisan migrate the database populates just fine.

The corresponding model is simple:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Foo extends Model
{
    protected $table = 'foo';

    protected $fillable = ['foo'];

}

I have a ModelFactory as well:

$factory->define(App\Foo::class, function (Faker\Generator $faker) {

    return [
        'foo' => $faker->email,
    ];
});

Tinker does what I think it should do when I try to make a flyer from the factory:

>>> factory('App\Foo')->make();
=> App\Foo {#696
     foo: "morgan39@gmail.com",

But when I try to hit the database, Eloquent fails to wrap the query values in a string:

>>> $foo = factory('App\Foo')->create();
Illuminate\Database\QueryException with message 'SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'foo' at row 1 (SQL: insert into `foo` (`foo`, `updated_at`, `created_at`) values (jakayla62@streich.org, 2016-09-06 23:59:03, 2016-09-06 23:59:03))'

Nothing like this on Google. Any ideas?

(Edited to show the same problem with a much simpler example)

  • 写回答

2条回答 默认 最新

  • dtxw20878 2016-09-07 00:35
    关注

    Solved it. The problem was the faker library. Apparently when you explicitly cast (string)$faker->whatever it fixes the problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘