У меня есть таблица, в которой я динамически добавляю строки, используя JS, который работает нормально, я могу добавить эти текстовые поля в БД, также используя цикл for, теперь одна из строк должна быть загрузчиком изображений, по текущемуКод Я могу сохранить только одно изображение, остальные изображения из разных строк не сохраняются, пожалуйста, помогите.Спасибо.
файл блейда:
<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]],
]);
}