請幫助我。我想問:Laravel查詢和陣列
假設我有2個表格:user_master
和people
。
現在我正在使用Laravel 5.1框架在PHP中構建一個應用程序,從user_master
表中選擇數據(在where
子句中有一些約束)並插入到people
表中。
的代碼是:
public function handle() {
$results = DB::select(DB::raw("SELECT * FROM user_master WHERE div_id = 1"));
foreach ($results as $res) {
//saving to array
//insert query to table people here.
}
}
我的問題是:
- 如何保存
select
查詢到陣列的結果,並 - 使用插入數組到
people
表RAW查詢(INSERT INTO people VALUES (...)
)。
P.S.:我的查詢是RAW查詢,不是使用雄辯。請提供沒有雄辯的答案。
非常感謝你的回答。
任何使用RAW查詢的具體原因。 1)需要使用' - > get();'方法在你的'$ result'變量中得到結果 –
你可以使用array_push()來創建一個foreach循環的數組 – lewis4u
@ lewis4u:你能給我舉個例子嗎? ? – joshua14