mod*_*i.n -2 php laravel lumen
我使用 JOIN 获取了一些记录,然后在数组中获取输出。我试图使用 insertOrIgnore() 和 implode() 将这些记录放入另一个表中。下面是我写的一段代码: 代码:
$strSql = DB::table('details')
->join("students", 'details.name', '=', 'students.name')
->select('details.id','students.contact')
->get();
foreach($strSql as $values){
$arrValues[] = "[ 'id' => '$values->id', 'contact' => $values->contact ]";
}
DB::table('Students_details')->insertOrIgnore([
(implode( ', ' , $arrValues))
]);
Run Code Online (Sandbox Code Playgroud)
错误:无法识别列。
SQLSTATE[42703]:未定义列:7 错误:关系“Students_details”的列“0”不存在第 1 行:在发生冲突时插入“Students_details”(“0”)值($1)... ^(SQL:在发生冲突时插入“Students_details”(“0”)值([ 'id' => '3', 'contact' => 232453876 ], [ 'id' => 'Mark', 'contact' => 567085643 ])没做什么)
看起来你把事情过于复杂化了。InsertIgnore要么接受一个键/值对数组,要么接受一个键/值对数组数组,因此您不需要创建该数组的字符串表示形式,然后将其内爆。
您目前的代码不会创建键/值对的嵌套数组,因此它将假设数组的数字键实际上是列名,而该列的值是数据的字符串版本。
如果您想让查询与现在类似,您可以执行以下操作:
$results = DB::table('details')
->join("students", 'details.name', '=', 'students.name')
->select('details.id', 'students.contact')
->get()
->map(function ($item) {
return (array)$item;
})
->toArray();
DB::table('Students_details')->insertOrIgnore($results);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1606 次 |
| 最近记录: |