Laravel 5.6-使用php artisan db:seed

时间:2018-12-09 16:42:11

标签: php database laravel-5.6

我正在尝试在DatabaseSeeder的帮助下插入多个数据。但是我遇到了这个错误。任何帮助表示赞赏。

Seeding: BorgerNotatsTableSeeder

   Illuminate\Database\QueryException  : Array to string conversion (SQL: insert into `borger_notats` (`user_id`, `borger_id`, `titel`, `beskrivelse`, `closed`, `updated_at`, `created_at`) values (1, 1, Autem ipsa praesentium ducimus unde., Ipsum fugiat ab culpa aut., 0, 2018-12-09 16:00:59, 2018-12-09 16:00:59))

  at /Users/sajidlatif/Code/humancare-laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

  1   ErrorException::("Array to string conversion")
      /Users/sajidlatif/Code/humancare-laravel/vendor/laravel/framework/src/Illuminate/Database/MySqlConnection.php:80

  2   PDOStatement::bindValue()
      /Users/sajidlatif/Code/humancare-laravel/vendor/laravel/framework/src/Illuminate/Database/MySqlConnection.php:80

  Please use the argument -v to see more details.

我的迁移表如下

public function up()
    {
        Schema::create('borger_notats', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('borger_id');
            $table->string('titel');
            $table->string('beskrivelse');
            $table->boolean('closed')->default(0);
            $table->timestamps();
        });
    }

我的UserFactory看起来像这样

$factory->define(App\BorgerNotat::class, function (Faker $faker) {
    return [
        'user_id' => 1,
        'borger_id' => 1,
        'titel' => $faker->sentences(10),
        'beskrivelse' => $faker->sentences(10),
        'closed' => $faker->boolean($chanceOfGettingTrue = 10)
    ];
});

我的BorgerNotatTableSeeder看起来像这样

class BorgerNotatsTableSeeder extends Seeder
{
   
    public function run()
    {
        factory(App\BorgerNotat::class, 7)->create();
    }
}

我的DatabaseSeeder看起来像这样

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        // $this->call(UsersTableSeeder::class);
        $this->call(BorgerNotatsTableSeeder::class);
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。 看来“句子”的UserFactory有一个数组,所以我不得不使用“句子”。因此,我删除了$ faker-> sentences(10)并将其替换为$ faker-> sentences(10),它可以正常工作。

$factory->define(App\BorgerNotat::class, function (Faker $faker) {
return [
    'user_id' => 1,
    'borger_id' => 1,
    'titel' => $faker->sentence(10),
    'beskrivelse' => $faker->sentence(10),
    'closed' => $faker->boolean($chanceOfGettingTrue = 10)
];
});