2017-04-01 47 views
0

當試圖播種表時,它顯示'數組到字符串轉換'錯誤。在播種表時向數組進行字符串轉換

這是錯誤的輸出:

[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)) 

表結構:

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 
     ) 

) 
+1

'print_r($ review)'看看。 –

+0

你的問題屬於'插入'過程,你提到了一個選擇過程,你如何插入你的數據? – hassan

+0

@u_mulder,附加到說明 – Ben

回答

1

正如你看到有多個問題在這裏,例如$name不成立的名字,但它擁有的數組,你應該使用:

$name = User::find($i + 9)->name; 

代替。

$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)) 

它過於複雜。

你可以在這裏使用這樣的事情:

'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條記錄,它不會有太大的區別,但它會做成千上萬)。

相關問題