我有一张表,其中可以使用JS动态添加行,效果很好,我也可以使用for循环将这些文本字段添加到db,现在,其中一行必须是图像上传器,当前代码我只能保存一个图像,不同行中的其余图像不保存,请帮助。谢谢。
刀片文件:
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Tags</th>
<th>Image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="button" class="btn-link cursor-pointer" value="Add Menu Item" href="javascript:void(0);">
<input type="hidden" class="tbl_row" name="count_row[]" id="count_row[]" value="1"/>
</td>
<td>
{{ Form::text('item_name[]', null, ['id' => 'name', 'class' => 'form-control'] ) }}
</td>
<td>
{{ Form::text('price[]', null, ['id' => 'price', 'class' => 'form-control'] )}}
</td>
<td>
{{ Form::select('tags[]', $tags, null, ['class' => 'form-control']) }}
</td>
<td>
<input type="file" name="image[]">
</td>
<td>
{{ Form::textarea('description[]', null, ['id' => 'description', 'rows' => 2, 'class' => 'form-control']) }}
</td>
</tr>
</tbody>
</table>
控制器:
$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');
for ($ln = $start_count; $ln < $end_count; $ln++) {
if (isset($attachment[$i]) && is_file($attachment[$i])) {
$fileNameWithExt = $attachment[$i]->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $attachment[$i]->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . $extention;
// Upload Image
$path = $attachment[$i]->storeAs('public/content-images/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$item = new MenuItem([
'category_id' => $category->id,
'name' => $request->item_name[$ln],
'price' => $request->price[$ln],
'description' => $request->description[$ln],
'status' => 1,
'image' => $fileNameToStore[$ln], **even the name is not saving properly in db**
]);
$item->save();
DB::table('menu_item_tag')->insert([
['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
]);
}
答案 0 :(得分:0)
由于azeós的注释设法通过将代码更改为此来解决,因此必须将$ i更改为$ ln,将fileNameToStore [$ ln]更改为fileNameToStore,以便将适当的文件名保存到db:
$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');
for ($ln = $start_count; $ln < $end_count; $ln++) {
if (isset($attachment[$ln]) && is_file($attachment[$ln])) {
$fileNameWithExt = $attachment[$ln]->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $attachment[$ln]->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . '.' . $extention;
// Upload Image
$path = $attachment[$ln]->storeAs('public/menu/'.$category->id.'/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$item = new MenuItem([
'category_id' => $category->id,
'name' => $request->item_name[$ln],
'price' => $request->price[$ln],
'description' => $request->description[$ln],
'status' => 1,
'image' => 'menu/' . $category->id . '/' . $fileNameToStore,
]);
$item->save();
DB::table('menu_item_tag')->insert([
['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
]);
}