在Laravel 5.3中批量插入

时间:2016-10-13 05:30:45

标签: laravel laravel-5.2 laravel-5.3

$list = [];

foreach($RoleDetails["Data"]["Permissions"] as $Permission) {
    $MyModel = new UserRolePermissionModel();
    $MyModel->UserID                  = $User->UserID;
    $MyModel->RolePermissionID        = $Permission->RolePermissionID;
    $MyModel->IsActive                = $Permission->IsActive;

    array_push($list, $MyModel);
}
\DB::table('tbluserrolepermission')->insert($list); 

以下是错误详情

QueryException {#293 ▼
  #sql: "insert into `tbluserrolepermission` (`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  #bindings: array:18 [▶]
  #message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `tbluserrolepermission` (`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`) values ({"UserID":21,"RolePermissionID":19,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":20,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":21,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":22,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":23,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":24,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":25,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":26,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":27,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":28,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":29,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":30,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":31,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":32,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":33,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":34,"IsActive":1,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":35,"IsActive":1,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":36,"IsActive":1,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}))"
  #code: "42S22"
  #file: "C:\xampp\htdocs\AS4\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
  #line: 761
  -previous: PDOException {#356 ▶}
  +errorInfo: array:3 [▶]
  +"previous": PDOException {#356 ▶}
  -trace: {▶}
}

3 个答案:

答案 0 :(得分:5)

它不起作用,因为您没有提供只有与列名匹配的值的数组数组来填充数据库。

示例

假设您要填充一组用户并将其保存到数据库中。以下示例将执行此操作。

email

请注意,上例中的votesusers I believe, you are trying to change\update the existing password with new password.And this is happening only when the user has been logged in. So, here you don't need "AuthStateListener". Just use this function: void ChangePassword(){ EditText newPassword = null; FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); user.updatePassword(newPassword.getText().toString().trim()) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Toast.makeText(getApplication(), "Password is updated!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplication(), "Failed to update password!", Toast.LENGTH_SHORT) .show(); } } }); } If successful proceed or handle the else part as required. 表中的2列。另请注意,只有其他数组的数组才会传递给insert方法。您要插入的内容实际上是一组雄辩的模型对象。

来源:https://laravel.com/docs/5.3/queries#inserts

答案 1 :(得分:3)

我把它修好如下。

foreach($RoleDetails["Data"]["RolePermissions"] as $RolePermission) {
    $data = [
        'UserID'                => $User->UserID,
        'RolePermissionID'      => $RolePermission->RolePermissionID,
        'IsActive'              => $RolePermission->IsActive,
        'IsProtectionAvailable' => $RolePermission->IsProtectionAvailable,
        'IsProtectionReadOnly'  => $RolePermission->IsProtectionReadOnly
    ];
    array_push($list,$data);
}
\DB::table('tbluserrolepermission')->insert($list); 

或者它可能是这样的。

UserRolePermissionModel::insert($list);

答案 2 :(得分:0)

你能试试吗?

UserRolePermissionModel::insert($list->toArray());

OR 

DB::table('table')->insert($list->toArray());