laravel 5.3 querybuilder db 插入使用数组迭代作为列

laravel 5.3 querybuilder db insert using array iteration as column

我是 Laravel 的新手,在 PHP 方面充其量只是个业余爱好者,但我正在努力学习。这是我的第一个问题,因为我通常能够通过搜索找到答案。

这可能是我忽略的简单问题。我正在提交一个表单,该表单最多可以有 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[42S22]:未找到列:1054 'field list' 中的未知列“0”(SQL:插入 primary_votes0, 1) 值 (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)) 在 Connection.php 第 761 行 在 Connection->运行QueryCallback('insert into primary_votes (0, 1) values (?, ?), (?, ?), (?, ?), (?, ?), (?, ?)', array('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(闭包) ) 在 Connection.php 行 717 在 Connection->运行('insert into primary_votes (0, 1) values (?, ?), (?, ?), (?, ?), (?, ?), (?, ?)', array('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 第 481 行 在 Connection->statement('insert into primary_votes (0, 1) values (?, ?), (?, ?), (?, ?), (?, ?), (?, ?)', array('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')) Connection.php 第435行 在 Connection->insert('insert into primary_votes (0, 1) values (?, ?), (?, ?), (?, ?), (?, ?), (?, ?)', array('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')) Builder.php 行 2117 在 Builder->insert(array('member_user_id' => array('1', '1'), 'candidates_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 行

所以,出于某种我无法弄清楚的原因,它正在尝试使用数组 "row" 键作为列名。为什么它将单个行键解释为数据库中的一列?提前致谢。

您的代码应如下所示:

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.

中插入数组