Laravel 5.3,PHP 5.6
新鲜的laravel new项目,最少的配置。

我做了一个简单的迁移和模型,并试图通过php artisan tinker将数据植入其中。

我的迁移如下所示:

<?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');
    }
}

当我运行php artisan migrate时,数据库填充就好了。

相应的模型很简单:
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    protected $fillable = ['foo'];

}

我也有一个ModelFactory:
$factory->define(App\Foo::class, function (Faker\Generator $faker) {

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

当我尝试对工厂的传单进行make编码时,Tinker做了我认为应该做的事情:
>>> factory('App\Foo')->make();
=> App\Foo {#696
     foo: "morgan39@gmail.com",

但是,当我尝试访问数据库时,Eloquent无法将查询值包装在字符串中:
>>> $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))'

Google上没有这样的东西。有任何想法吗?

(编辑以通过一个更简单的示例来显示相同​​的问题)

最佳答案

默认情况下,某些造假方法将其结果作为数组返回。例如:

$faker->words(3)

会返回:
array('porro', 'sed', 'magni')

要以字符串形式返回结果,您可以将true作为某些方法的第二个参数传递,如在上一个示例中使用words方法:
$faker->words(3, true)

返回:
"porro sed magni"

readme中所述:
words($nb = 3, $asText = false)    // array('porro', 'sed', 'magni')

09-05 11:27
查看更多