Yes ... what's the problem ? You can only upload one file ? Do you have any error message ? Can you detail your problem please ?
Oct 21, 2022
15
Level 1
Inertiajs : Upload Multiple images
hi, i want to upload more than one image in my laravel-ineria-vue application
<template>
<div class="newservice">
<form @submit.prevent="submit" enctype="multipart/form-data">
<div class="new__service__form">
<div class="new__form__group new__group">
<span>الصور</span>
<input @input="form.gallery = $event.target.files[0]"
type="file"
multiple="multiple"
accept="image/jpg, image/jpeg, image/png"
/>
<p v-if="$page.props.errors.gallery">
{{ $page.props.errors.gallery }}
</p>
</div>
<div class="new__form__group">
<button type="submit">save</button>
</div>
</div>
</form>
</div>
</template>
<script setup>
import {useForm} from "@inertiajs/inertia-vue3"
let form = useForm({
gallery: [],
});
let submit = () => {
form.post('/admin/services/new');
}
</script>
public function store(Request $request) {
$request->validate([
"gallery" => "required",
]);
if($request->hasfile('gallery')){
$_images_ = array();
foreach($request->file('gallery') as $imgs)
{
$imgs_newname = Str::random(8) . '.' . $imgs->getClientOriginalExtension();
$imgs->move('image/service/',$imgs_newname);
$_images_[] = $imgs_newname;
}
}
$service__images = implode("-",$_images_);
return $service__images;
}
what's the problem?
Level 8
@oncebar Thank you. As you can see the "Gallery" is an instance of "UploadedFile". Your script is expecting an array of UploadedFiles to loop over in the foreach loop.
The problem is:
<input @input="form.gallery = $event.target.files[0]"
In this row, you override form.gallery with the first (key) file.
You should remove the key to pass through the full array of files.
<input @input="form.gallery = $event.target.files"
If you change this, it wil work.
3 likes
Please or to participate in this conversation.