2016-09-03 48 views
1

我是Laravel和業餘充其量使用PHP,但嘗試學習。這是我的第一個問題,因爲我通常能夠找到答案搜索。laravel 5.3使用數組迭代作爲列的querybuilder數據庫插入

這可能是我忽略的簡單東西。我提交的表單最多可以有2行從視圖到控制器。我創建從數據數組,像這樣在我的控制器:

foreach($candidates_member_id as $key => $value) 
    { 
     $arrData[] = array( 
      'member_user_id'  => $member_user_id[$key], 
      'candidates_member_id' => $candidates_member_id[$key], 
      'candidates_district' => $candidates_district[$key], 
      'candidate_name'  => $candidate_name[$key], 
      'created_at'   => $created_at[$key], 
      'updated_at'   => $updated_at[$key]       
     ); 
    } 

如果我的print_r($ arrData)上的相關看法,我得到的結果是這樣的:

Array 
(
    [0] => Array 
     (
      [member_user_id] => 1 
      [candidates_member_id] => 12345 
      [candidates_district] => 6 
      [candidate_name] => Doe, John 
      [created_at] => 2016-09-03 15:07:14 
      [updated_at] => 2016-09-03 15:07:14 
     ) 

    [1] => Array 
     (
      [member_user_id] => 1 
      [candidates_member_id] => 54321 
      [candidates_district] => 6 
      [candidate_name] => Doe, Jane 
      [created_at] => 2016-09-03 15:07:15 
      [updated_at] => 2016-09-03 15:07:15 
     ) 
) 

然而,當我添加DB插入到上面的foreach循環:

DB::table('primary_votes')->insert(
      ['member_user_id' => $member_user_id, 
      'candidates_member_id' => $candidates_member_id, 
      'candidates_district' => $candidates_district, 
      'created_at' => $created_at, 
      'updated_at' => $updated_at] 
     ); 

我得到以下錯誤:

SQLSTATE [42S2 2]:未找到列:1054'字段列表'中的未知列'0'(SQL:插入到primary_votes0,1)值(1,1),(12345,54321),(6,6), -09-03 15:07:14,2016-09-03 15:07:15),(2016-09-03 15:07:14,2016-09-03 15:07:15)) in Connection。 (?,?),(?,?),(?,?),(?,?),(?,?)'的插入到primary_votes0,1)的php行761 。 ,數組('1','1','12345','54321','6','6','2016-09-03 15:07:14','2016-09-03 15:07: 15','2016-09-03 15:07:14','2016-09-03 15:07:15'),object(Closure))Connection.php line 717 at Connection-> run('insert (',','),(?,?),(?,?),(?,?),(?,?)'的數值爲primary_votes0,1) 12345','54 321','6','6','2016-09-03 15:07:14','2016-09-03 15:07:15','2016-09-03 15:07:14', '插入到primary_votes0,1)的值(?,?),(?)中的Connection.php行481, 。 (','),(?,?),(?,?)',數組('1','1','12345','54321','6','6',' 2016-09-03 15:07:14','2016-09-03 15:07:15','2016-09-03 15:07:14','2016-09-03 15:07:15' )插入('插入到primary_votes0,1)值(?,?),(?,?),(?,?),(?,?),(?))中的Connection.php line 435 數組'('1','1','12345','54321','6','6','2016-09-03 15:07:14','2016-09- 03 15:07:15','2016-09-03 15:07:14','2016-09-03 15:07:15'))in Builder.php line 2117 at Builder-> insert(array( 'member_user_id'=>數組('1','1'),'c andidates_member_id'=> array('12345','54321'),'candidates_district'=> array('6','6'),'created_at'=> array('2016-09-03 15:07:14' ,'2016-09-03 15:07:15'),'updated_at'=> array('2016-09-03 15:07:14','2016-09-03 15:07:15')))在VoteSubmitController.php行60

所以,由於某種原因,我想不出,它試圖使用數組「行」鍵作爲列名。爲什麼它將單個行鍵解釋爲數據庫中的一列?提前致謝。

回答

2

您的代碼應該是這樣的:

foreach($candidates_member_id as $key => $value) 
{ 
    $arrData = array( 
     'member_user_id'  => $member_user_id[$key], 
     'candidates_member_id' => $candidates_member_id[$key], 
     'candidates_district' => $candidates_district[$key], 
     'candidate_name'  => $candidate_name[$key], 
     'created_at'   => $created_at[$key], 
     'updated_at'   => $updated_at[$key]       
    ); 
    DB::table('primary_votes')->insert($arrData); 
} 

原因MySQL錯誤是$ member_user_id,$ candidates_member_id是數組,而不是標量值。你不能在mysql中插入數組。

+0

非常感謝!越來越近。插入現在正在發生,但第一次迭代插入兩次。當我有機會時,我會嘗試調試。我有另一個插入作爲該功能的一部分,加上更新,我會爲每個人創建一個數組。 –