Does the images show up on your disk OR is it both that database and you disk that is missing the image?
Oct 31, 2019
7
Level 5
I can't upload multiple images in database
I want to uploads multiple images in my database!
Problem is when I submit after selecting multiple images then only one image Uploaded in my database...
Here are my View and Controller
Controller
if ($request->hasFile('image')){
foreach ($request->file('image') as $image){
$fileName = "Photos_".str_random(5).'.'.$image->getClientOriginalExtension();
$image->storeAs('uploads',$fileName);
Image::create([
'name' => $fileName,
'album_id' =>random_int(1,5)
]);
$this->setSuccessMsg("Image Upload Successful!");
return redirect()->back();
}
}
VIEW
<div class="form-group row d-flex align-items-center mb-5">
<label for="image" class="col-lg-3 form-control-label">Image</label>
<div class="col-lg-9">
<input type="file" name="image[]" id="image" class="form-control" multiple>
</div>
</div>
Level 5
Problem is solved by myself ....................
if ($request->hasFile('image')){
foreach ($request->file('image') as $image){
$fileName = "Photos_".str_random(5).'.'.$image->getClientOriginalExtension();
$image->storeAs('uploads',$fileName);
Image::create([
'name' => $fileName,
'album_id' =>random_int(1,5)
]);
$this->setSuccessMsg("Image Upload Successful!");
return redirect()->back();
}
}
Here I Return my value inside foreach that's why after one image insert it redirect back......
CODE MUST BE LIKE THIS
if ($request->hasFile('image')){
foreach ($request->file('image') as $image){
$fileName = "Photos_".str_random(5).'.'.$image->getClientOriginalExtension();
$image->storeAs('uploads',$fileName);
Image::create([
'name' => $fileName,
'album_id' =>random_int(1,5)
]);
$this->setSuccessMsg("Image Upload Successful!");
}
return redirect()->back();
}
Please or to participate in this conversation.