laravel播种器数组到字符串转换

时间:2017-05-23 13:31:09

标签: laravel

我有一个像这样的模型工厂

$factory->define(App\Sale::class, function (Faker\Generator $faker) {
return [
    'unit' => $faker->randomDigit,
    'street_no' => $faker->randomDigit,
    'street_name' => $faker->streetName,
    'street_type' => $faker->streetSuffix,
    'suburb' => $faker->randomElements(['Melton South','Melton West','Rye']),
    'postcode' => $faker->numberBetween($min=1000, $max=4000),
    'sale_date' => $faker->dateTimeThisYear,
];
});

数据库播种器运行它

factory(App\Sale::class, 5)->create();

问题是当我运行它时php artisan db:seed我收到了错误

  [Illuminate\Database\QueryException]
  Array to string conversion (SQL: insert into `sales` (`unit`, `street_no`,
  `street_name`, `street_type`, `suburb`, `postcode`, `sale_date`, 
`updated_at`, `created_at`) 
values (7, 1, Labadie Centers, Bridge, Rye, 3758, 2016-08-12 1
  5:02:07, 2017-05-23 13:16:56, 2017-05-23 13:16:56))

错误sql没有显示我能看到的任何数组。

当我将该sql粘贴到我的db应用程序并运行它时,为了使其工作,我必须引用所有字符串,但laravel docs在使用faker时不说任何关于它的内容? 我在模型工厂中遗漏了什么吗?

EXI

1 个答案:

答案 0 :(得分:6)

尝试更改

'suburb' => $faker->randomElements(['Melton South','Melton West','Rye']),

'suburb' => $faker->randomElement(['Melton South','Melton West','Rye']),