Laravel:insertOrIgnore()不接受数组作为输入

问题描述 投票:0回答:1

我已经使用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))
]); 

错误:列无法识别。

SQLSTATE [42703]:未定义的列:7错误:关系“ Students_details”的列“ 0”不存在第1行:发生冲突时,将值($ 1)插入“ Students_details”(“ 0”)值中...^(SQL:插入到“ Students_details”(“ 0”)中,值(['id'=>'3','contact'=> 232453876],['id'=>'Mark','contact'=> 567085643 ])在冲突上什么都不做)

php laravel lumen
1个回答
0
投票

似乎您已经使事情复杂化了。 InsertIgnore要么采用键/值对的数组,要么采用键/值对的数组的数组,因此您无需创建该数组的字符串表示形式然后内爆。

如果您想使查询与现在的查询相似,可以执行以下操作:

$results = DB::table('details')
    ->join("students", 'details.name', '=', 'students.name')
    ->select('details.id', 'students.contact')
    ->get()
    ->map(function ($item) {
        return (array)$item;
    });

DB::table('Students_details')->insertOrIgnore($results);
© www.soinside.com 2019 - 2024. All rights reserved.