2017-06-21 94 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