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

发布于 2022-09-04 04:21:42 字数 2777 浏览 13 评论 0

// 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))'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文