播种 table 时数组到字符串的转换
Array to string conversion when seeding a table
尝试为 table 播种时,显示 'array to string conversion' 错误。
这是错误的输出:
[Illuminate\Database\QueryException]
Array to string conversion (SQL: insert into `reviews` (`user_id`, `name`, `location`, `header`, `comments`, `identifier`, `stars`, `privacy`, `actioned`, `appr
oved`, `created_at`) values (10, Bradley Davis, Paulastad, Commodi quas expedita eum., Voluptas magni iusto nemo ea vitae harum quasi., 6c621319-c6ba-41c1-bb0b-696b602470
ef, 5, 1, 0, 1, 2017-03-25 11:44:12))
Table结构:
Schema::create('reviews', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name')->nullable();
$table->string('location')->nullable();
$table->string('header')->nullable();
$table->string('comments')->nullable();
$table->uuid('identifier');
$table->integer('stars')->nullable();
$table->boolean('privacy')->default('1');
$table->boolean('actioned')->default('0');
$table->boolean('approved')->default('0');
$table->nullableTimestamps();
});
种子函数:
for ($i = 1; $i < 41; $i++) {
$name = User::where('id', $i + 9)->pluck('name')->all();
$uuid = Uuid::generate(4);
$review = [
'user_id' => $i + 9,
'name' => $name,
'location' => $faker->city,
'header' => $faker->sentence($nbWords = 3, $variableNbWords = true),
'comments' => $faker->text($maxNbChars = 50),
'identifier' => $uuid,
'stars' => [1, 2, 3, 4, 5][rand(0, count([1, 2, 3, 4, 5]) - 1)],
'privacy' => [1, 0][rand(0, count([1, 0]) - 1)],
'actioned' => [1, 0][rand(0, count([1, 0]) - 1)],
'approved' => [0, 1][rand(0, count([0, 1]) - 1)],
'created_at' => Carbon::now()->subWeeks(array_rand([2, 3, 4, 5, 6, 7], 1))
];
}
DB::table('reviews')->insert($review);
print_r($评论):
Array
(
[user_id] => 10
[name] => Array
(
[0] => David Robertson
)
[location] => Lake Suzannechester
[header] => Repellendus aut alias exercitationem.
[comments] => Sunt non temporibus pariatur totam aut qui.
[identifier] => Webpatser\Uuid\Uuid Object
(
[bytes:protected] => �a�?�IB��"�mLC�
[hex:protected] =>
[string:protected] => d51961bb-3fcf-4942-a0d9-22a56d4c4392
[urn:protected] =>
[version:protected] =>
[variant:protected] =>
[node:protected] =>
[time:protected] =>
)
[stars] => 4
[privacy] => 1
[actioned] => 1
[approved] => 1
[created_at] => Carbon\Carbon Object
(
[date] => 2017-03-04 12:04:01.225487
[timezone_type] => 3
[timezone] => UTC
)
)
如你所见,这里有多个问题,例如 $name
不包含名称,但它包含数组,你应该使用:
$name = User::find($i + 9)->name;
相反。
类似的是 $uuid
- 它应该 return 字符串而不是一些对象。
另外当我看这部分的时候:
'stars' => [1, 2, 3, 4, 5][rand(0, count([1, 2, 3, 4, 5]) - 1)],
'privacy' => [1, 0][rand(0, count([1, 0]) - 1)],
'actioned' => [1, 0][rand(0, count([1, 0]) - 1)],
'approved' => [0, 1][rand(0, count([0, 1]) - 1)],
'created_at' => Carbon::now()->subWeeks(array_rand([2, 3, 4, 5, 6, 7], 1))
太复杂了。
你可以在这里使用这样的东西:
'stars' => rand(1, 5),
'privacy' => rand(0, 1),
'actioned' => rand(0, 1),
'approved' => rand(0, 1),
'created_at' => Carbon::now()->subWeeks(rand(2, 7))->toDateTimeString(),
还有:
DB::table('reviews')->insert($review);
你在循环外使用它吗?如果是这样,它没有多大意义,因为在每个循环 运行 中你再次设置它。但是,如果您在循环内执行此操作,您还可以将其优化为不 运行 如此多的插入(显然,对于 41 条记录,它不会产生太大差异,但对于数千条记录,它会产生影响)。
尝试为 table 播种时,显示 'array to string conversion' 错误。
这是错误的输出:
[Illuminate\Database\QueryException]
Array to string conversion (SQL: insert into `reviews` (`user_id`, `name`, `location`, `header`, `comments`, `identifier`, `stars`, `privacy`, `actioned`, `appr
oved`, `created_at`) values (10, Bradley Davis, Paulastad, Commodi quas expedita eum., Voluptas magni iusto nemo ea vitae harum quasi., 6c621319-c6ba-41c1-bb0b-696b602470
ef, 5, 1, 0, 1, 2017-03-25 11:44:12))
Table结构:
Schema::create('reviews', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name')->nullable();
$table->string('location')->nullable();
$table->string('header')->nullable();
$table->string('comments')->nullable();
$table->uuid('identifier');
$table->integer('stars')->nullable();
$table->boolean('privacy')->default('1');
$table->boolean('actioned')->default('0');
$table->boolean('approved')->default('0');
$table->nullableTimestamps();
});
种子函数:
for ($i = 1; $i < 41; $i++) {
$name = User::where('id', $i + 9)->pluck('name')->all();
$uuid = Uuid::generate(4);
$review = [
'user_id' => $i + 9,
'name' => $name,
'location' => $faker->city,
'header' => $faker->sentence($nbWords = 3, $variableNbWords = true),
'comments' => $faker->text($maxNbChars = 50),
'identifier' => $uuid,
'stars' => [1, 2, 3, 4, 5][rand(0, count([1, 2, 3, 4, 5]) - 1)],
'privacy' => [1, 0][rand(0, count([1, 0]) - 1)],
'actioned' => [1, 0][rand(0, count([1, 0]) - 1)],
'approved' => [0, 1][rand(0, count([0, 1]) - 1)],
'created_at' => Carbon::now()->subWeeks(array_rand([2, 3, 4, 5, 6, 7], 1))
];
}
DB::table('reviews')->insert($review);
print_r($评论):
Array
(
[user_id] => 10
[name] => Array
(
[0] => David Robertson
)
[location] => Lake Suzannechester
[header] => Repellendus aut alias exercitationem.
[comments] => Sunt non temporibus pariatur totam aut qui.
[identifier] => Webpatser\Uuid\Uuid Object
(
[bytes:protected] => �a�?�IB��"�mLC�
[hex:protected] =>
[string:protected] => d51961bb-3fcf-4942-a0d9-22a56d4c4392
[urn:protected] =>
[version:protected] =>
[variant:protected] =>
[node:protected] =>
[time:protected] =>
)
[stars] => 4
[privacy] => 1
[actioned] => 1
[approved] => 1
[created_at] => Carbon\Carbon Object
(
[date] => 2017-03-04 12:04:01.225487
[timezone_type] => 3
[timezone] => UTC
)
)
如你所见,这里有多个问题,例如 $name
不包含名称,但它包含数组,你应该使用:
$name = User::find($i + 9)->name;
相反。
类似的是 $uuid
- 它应该 return 字符串而不是一些对象。
另外当我看这部分的时候:
'stars' => [1, 2, 3, 4, 5][rand(0, count([1, 2, 3, 4, 5]) - 1)],
'privacy' => [1, 0][rand(0, count([1, 0]) - 1)],
'actioned' => [1, 0][rand(0, count([1, 0]) - 1)],
'approved' => [0, 1][rand(0, count([0, 1]) - 1)],
'created_at' => Carbon::now()->subWeeks(array_rand([2, 3, 4, 5, 6, 7], 1))
太复杂了。
你可以在这里使用这样的东西:
'stars' => rand(1, 5),
'privacy' => rand(0, 1),
'actioned' => rand(0, 1),
'approved' => rand(0, 1),
'created_at' => Carbon::now()->subWeeks(rand(2, 7))->toDateTimeString(),
还有:
DB::table('reviews')->insert($review);
你在循环外使用它吗?如果是这样,它没有多大意义,因为在每个循环 运行 中你再次设置它。但是,如果您在循环内执行此操作,您还可以将其优化为不 运行 如此多的插入(显然,对于 41 条记录,它不会产生太大差异,但对于数千条记录,它会产生影响)。