I ran into a problem in Laravel, I'm trying to add multiple images at the same time using the multiple attribute on an input of type file:
<input type="file" name="image[]" id="image" multiple class="form-control">
If, for example, I select 3 images and send it, then I return it in the store method:
return $files = $request->file('image');
it shows me as follows:
[
{},
{},
{}
]
and when accessing any of the properties of that object, it returns me (varies according to the position I access):
D:\xampp\tmp\php698A.tmp
I then convert to a valid path with:
$upload->upload_global($files[0], 'productimage');
And it returns me:
/uploads/productimage/1552657379_1552657379.jpg
So far everything is correct, what I would like to know is how to register all the images. I have used for and foreach and none works for me, it only saves the first image:
for:
$files = $request->file('image');
for ($i=0; $i < count($files) ; $i++) {
$this->modelo::create([
'title' => $request->title,
'product_id' => base64_decode($request->product_id),
'image' => $upload->upload_global($file, 'productimage'),
'create_uid' => Auth::user()->id,
'write_uid' => Auth::user()->id
]);
}
foreach:
$files = $request->file('image');
foreach($files as $file){
$this->modelo::create([
'title' => $request->title,
'product_id' => base64_decode($request->product_id),
'image' => $upload->upload_global($file, 'productimage'),
'create_uid' => Auth::user()->id,
'write_uid' => Auth::user()->id
]);
}
And in both cases it only saves the first image. That means it doesn't loop through the $i, but it always uploads the same image repeatedly depending on how many images I select. How could I get all the images uploaded? From already thank you very much.
you should do it like this: