I tried using a foreach loop to iterate through the request files, but only the last image in the loop is saved to the database and file system. How can I fix this? any help would be appreciated,
here is my code
$galleryArr = [];
$allowedExtensions = ['jpg', 'png', 'jpeg'];
$counter = 1;
if ($req->hasFile('images')) {
foreach ($req->file('images') as $file) {
$fileExtension = $file->getClientOriginalExtension();
$fileName = $currentTimestamp . "_" . $counter . "." . $fileExtension;
// Handle thumbnail creation
$this->generateProductThumbnailImage($file, $fileName);
// Add to gallery array
$galleryArr[] = $fileName;
$counter++;
}
}
// Convert gallery array to comma-separated string
$products->images = implode(',', $galleryArr);
// Save the product data
$products->save();
public function GenerateProductThumbnailImage($image, $imageName) {
$destinationPathThumbnail = public_path('img/prod/thumbnail');
$destinationPath = public_path('img/prod');
$img = Image::make($image->path());
// Save the original image
$img->resize(540, 689, function($constraint) { $constraint->aspectRatio(); })->save($destinationPath . "https://stackoverflow.com/" . $imageName);
// Save the thumbnail image
$img->resize(104, 104, function($constraint) { $constraint->aspectRatio(); })->save($destinationPathThumbnail . "https://stackoverflow.com/" . $imageName);
}