Laravel 5.3.23 Model Factory can make( ), but can NOT create( )

// database/migrations/create_flyers_table.php

public function up() {
    Schema::create('flyers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('street');
        $table->string('city',40);
        $table->string('zip',10);
        $table->string('state',40);
        $table->string('country',40);
        $table->integer('price');
        $table->text('description');
        $table->timestamps();
    });
}

// app/Flyer.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flyer extends Model
{
    public function photos() {
        return $this->hasMany('App\Photo');
    }
}

// database/factories/ModelFactory.php

$factory->define(App\Flyer::class, function (Faker\Generator $faker) {
    return [
        'street' => $faker->streetAddress,
        'city' => $faker->city,
        'zip' => $faker->postcode,
        'state' => $faker->state,
        'country' => $faker->country,
        'price' => $faker->numberBetween(5000, 1000000),
        'description' => $faker->paragraphs(3),
    ];
});

// php artisan tinker

$flyer = factory('AppFlyer')->make();

=> App\Flyer {#703
     street: "708 Barney Pass Suite 718",
     city: "Linniefurt",
     zip: "45327-3918",
     state: "Tennessee",
     country: "Slovakia (Slovak Republic)",
     price: 715491,
     description: [
       "Ut id laudantium libero. Voluptates hic vel quidem eligendi ut facere voluptatibus. Vitae quo voluptatem voluptas consectetur autem.",
       "Error a eum rerum debitis fugit labore velit. Molestias esse est repellat ex necessitatibus iste temporibus. Aperiam sit sed facilis id repellat illum voluptatem.",
       "Autem consequatur quis qui repellat. Harum ducimus autem et sit. Vitae et laborum illum aliquam temporibus.",
     ],
   }

$flyer = factory('AppFlyer')->create();

IlluminateDatabaseQueryException with message 'PHP error: Array to string conversion in /Users/e/Documents/workspace/flyer/vendor/laravel/framework/src/Illuminate/Database/MySqlConnection.php on line 80 (SQL: insert into flyers (street, city, zip, state, country, price, description, updated_at, created_at) values (5520 Emmerich Greens, Lake Milford, 69898-4916, Arkansas, Martinique, 215879, Sit eos corrupti ipsa ut. Facere ut nesciunt omnis ea enim et nam. Ut vel cupiditate voluptatem delectus., 2016-11-18 13:05:40, 2016-11-18 13:05:40))'

阅读 1.9k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进