2017-06-21 96 views
0

我从检索到的一些数据DB使用:Laravel插入收集分贝

$filtered = DB::table('tmp_table')->distinct()->select('type','description','note')->get(); 

,我想插入我像另一个表已检索:

DB::table('tmp_other')->insert($filtered); 

,但我收到thie错误:

Type error: Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, object given 

这是最好的方法来做到这一点?

+0

'DB :: table('tmp_other') - > insert($ filtered-> toArray());'应该有效。 – Daan

+0

@StefanoMaglione好......你明白了,错误信息中写了什么? –

+0

Eloquent标签在这里不适用吗? – apokryfos

回答

2

的SQL的方式做到这blunk插入更多的东西像本地人:

DB::insert(
     "INSERT INTO tmp_other (type,description,note) 
     SELECT DISTINCT type,description,note FROM tmp_table" 
); 

这将避免整体出让到网络服务器/传输回SQL服务器进程。

+0

最简单的想法是最好的! –

0

你试试这个会做的伎俩这:)

$filtered = DB::table('tmp_table')->distinct()->select('type','description','note')->get(); 
$filtered->toArray(); 

DB::table('tmp_other')->insert($filtered); 
+1

我已经试过这个,但它不工作,类stdClass的对象不能转换为字符串 –

+0

请张贴您的代码我会研究它.. – 2017-06-21 14:03:22