如何将图像添加到 laravel 中的数组
How to add image to array in laravel
如何将图像添加到 laravel 中的数组?
我有这样的代码用于插入数据
$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);
我知道,如果我的代码有误,但我很困惑,如何将图像添加到数组?我使用 laravel 框架将数据插入数据库。
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
}
你应该使用path而不是绝对路径
如何将图像添加到 laravel 中的数组?
我有这样的代码用于插入数据
$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);
我知道,如果我的代码有误,但我很困惑,如何将图像添加到数组?我使用 laravel 框架将数据插入数据库。
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
}
你应该使用path而不是绝对路径