Reputation: 434
I'm trying to store multiple images to the database but it only stores one image. I don't see any errors. I have tried this but it stores images in public folder, I want the images to be stored in database. How can I fix this? any help would be appreciated.
Controller
if($request->hasFile('files')){
$files = $request->file('files');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$images = $file->store('public/photos');
}
}
ProductsPhoto::create([
'product_id' => $product->id,
'filename' => $images
]);
Blade
<input type="file" name="files[]">
Upvotes: 0
Views: 996
Reputation: 788
I see one issue on your code, you are moving images correctly but storing it in DB outside of loop, which will only store last iteration of loop in DB, you can use this code to store multiple images in DB
if($request->hasFile('files')){
$store_file = [];
$files = $request->file('files');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$images = $file->store('public/photos');
$store_file[] = [
'product_id' => $product->id,
'filename' => $images
];
}
ProductsPhoto::insert($store_file);
}
Upvotes: 1