我在foreach中遇到了語法錯誤。什麼是使用內部查詢生成器的陣列的foreach的正確方法,代碼如下:發生如何在Laravel的查詢生成器數組中使用foreach
public function postItems(Request $request, $id){
$itemid = \DB::table('items_tables')->where('category_id','=',$id)->get();
DB::table('store_items')->insert(
array('user_id' => \Auth::user()->id,
foreach($itemid as $itemids){
'items' => $itemids->name,
}
'store_id' => $id)
);
return view('actions.itemstore');
}
語法錯誤中的foreach如下:
Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')'
$ itemids->名稱變量有許多值。我需要循環這一點。
認真嗎?你沒有得到錯誤?你見過像你寫的foreach($ itemid as $ itemids)嗎?{ 'items'=> $ itemids-> name, } – rahulsm
我做了一些修改。請檢查一下。 –
@JaseelBavu你的代碼有太多錯誤。你正試圖用'insert()'結構錯誤的數組。你正在使用loop而不是'pluck()'。數據庫架構不好。錯誤的語法無處不在。你應該真的閱讀Laravel和PHP文檔。這會爲你節省很多時間。 –