laravel多次插入MySQL

时间:2016-04-06 19:10:20

标签: php mysql laravel

我试图将多行数据插入MySQL表中,但只插入了第一行。这是我的控制器中的代码。

DB :: table("users_has_activities") -> insert
(
array("usersID" => $userId, "activitiesID" => 1, "time" => $datetime, "value" => 1,
"multiplier" => 1 ),


array("usersID" => $userId, "activitiesID" => 2, "time" => $datetime, "value" => 1,
"multiplier" => 1 ),

array("usersID" => $userId, "activitiesID" => 3, "time" => $datetime, "value" => 1,
"multiplier" => 1 )
);

我可以这样做但我正在寻找更好的方法。

DB :: table("users_has_activities") -> insert
(
array("usersID" => $userId, "activitiesID" => 1, "time" => $datetime, "value" => 1,
"multiplier" => 1 )
);

  DB :: table("users_has_activities") -> insert
(
array("usersID" => $userId, "activitiesID" => 2, "time" => $datetime, "value" => 1,
"multiplier" => 1 )
);


DB :: table("users_has_activities") -> insert
(
array("usersID" => $userId, "activitiesID" => 3, "time" => $datetime,   "value" =>   1,
"multiplier" => 1 )
 );

1 个答案:

答案 0 :(得分:1)

您尝试传递数组,但忘记添加array()

DB::table("users_has_activities") -> insert(array(
array("usersID" => $userId, "activitiesID" => 1, "time" => $datetime, "value" => 1,
"multiplier" => 1 ),

array("usersID" => $userId, "activitiesID" => 2, "time" => $datetime, "value" => 1,
"multiplier" => 1 ),

array("usersID" => $userId, "activitiesID" => 3, "time" => $datetime, "value" => 1,
"multiplier" => 1 )
));