How to add image to array in laravel ?
I have code like this for insert data
$itemcodes = $request->itemcode;
foreach ($itemcodes as $i => $item_code) {
$order_detail[] = [
'warr_item_code' => $item_code,
'warr_item_category' => $request->itemcategory[$i],
'warr_item_name' => $request->itemname[$i],
'warr_item_qty' => $request->itemqty[$i],
'warr_item_condition' => $request->itemcondition[$i],
'warr_item_origin' => $request->itemorigin[$i],
'warr_item_purchase_year' => $request->itempurchaseyear[$i],
if ($request->hasfile($request->itemimg[$i])) {
$file = $request->file($request->itemimg[$i]),
$extension = $file->getClientOriginalExtension(),
$img = time() . '.' . $extension;
$file->move('dist/images/data-barang/', $img);
'warr_item_img' => $img;
} else {
return $request;
'warr_item_img' => '';
}
];
}
WarrentItemModel::insert($order_detail);
I know, if my code was wrong, but i so confused, how to add image to array ? i used laravel framework for insert data to database.
CodePudding user response:
foreach ($itemcodes as $i => $item_code) {
$tmp = [
'warr_item_code' => $item_code,
'warr_item_category' => $request->itemcategory[$i],
'warr_item_name' => $request->itemname[$i],
'warr_item_qty' => $request->itemqty[$i],
'warr_item_condition' => $request->itemcondition[$i],
'warr_item_origin' => $request->itemorigin[$i],
'warr_item_purchase_year' => $request->itempurchaseyear[$i],
];
if ($request->hasfile($request->itemimg[$i])) {
$file = $request->file($request->itemimg[$i]),
$extension = $file->getClientOriginalExtension(),
$img = time() . '.' . $extension;
$file->move('dist/images/data-barang/', $img);
$pathOfImage = 'dist/images/data-barang/' . $img;
$tmp['warr_item_img'] = $pathOfImage ;
} else {
$tmp['warr_item_img'] = '';
}
$order_detail[] = $tmp
}
You should use path instead of absolute path