我正在尝试更新图像,我的代码工作正常,但是当我上传新图像时。 我的图像路径是 public ssets\images\uploads\home 我正在存储图像
这是我的代码:
public function update(Request $request, $id)
{
$updatehomeitems = Home::find($id);
$request->validate([
'logo' => 'required|mimes:jpeg,png,jpg,gif,svg,webp|dimensions:width=160,height=40',
'image_1' => 'required|mimes:jpeg,png,jpg,gif,svg,webp|dimensions:width=960,height=700',
'image_2' => 'required|mimes:jpeg,png,jpg,gif,svg,webp|dimensions:width=960,height=700',
'image_3' => 'required|mimes:jpeg,png,jpg,gif,svg,webp|dimensions:width=1000,height=1000',
'description' => 'required',
], [
'logo.required' => 'The header logo field is required.',
'logo.mimes' => 'Please upload an image with a valid extension (jpeg, png, jpg, gif, svg, webp).',
'logo.dimensions' => 'The header logo must be 160 pixels in width and 40 pixels in height.',
'image_1.required' => 'The left image field is required.',
'image_1.mimes' => 'Please upload an image with a valid extension (jpeg, png, jpg, gif, svg, webp).',
'image_1.dimensions' => 'The left image must be 960 pixels in width and 700 pixels in height.',
'image_2.required' => 'The right image field is required.',
'image_2.mimes' => 'Please upload an image with a valid extension (jpeg, png, jpg, gif, svg, webp).',
'image_2.dimensions' => 'The right image must be 960 pixels in width and 700 pixels in height.',
'image_3.required' => 'The content image field is required.',
'image_3.mimes' => 'Please upload an image with a valid extension (jpeg, png, jpg, gif, svg, webp).',
'image_3.dimensions' => 'The content image must be 1000 pixels in width and 1000 pixels in height.',
'description.required' => 'Please enter a description.',
]);
$input = $request->all();
// Handle logo
if ($request->hasFile('logo')) {
$destinationPath = 'assets/images/uploads/home/';
$imageName = date('YmdHis') . $request->file('logo')->getClientOriginalExtension();
$request->file('logo')->move(public_path($destinationPath), $imageName);
// Delete old logo
if ($updatehomeitems->logo && file_exists(public_path($updatehomeitems->logo))) {
Home::delete(public_path($updatehomeitems->logo));
// unlink(public_path($updatehomeitems->logo));
}
$input['logo'] = $imageName; // Save only the image name
} else {
$input['logo'] = $updatehomeitems->logo;
}
// Handle image_1, image_2, image_3
for ($i = 1; $i <= 3; $i++) {
$imageField = 'image_' . $i;
if ($request->hasFile($imageField)) {
$destinationPath = 'assets/images/uploads/home/';
$imageName = date('YmdHis') . "_{$imageField}." . $request->file($imageField)->getClientOriginalExtension();
$request->file($imageField)->move(public_path($destinationPath), $imageName);
// Delete old image
if ($updatehomeitems->$imageField && file_exists(public_path($updatehomeitems->$imageField))) {
// unlink(public_path($updatehomeitems->$imageField));
Home::delete(public_path($updatehomeitems->$imageField));
}
$input[$imageField] = $imageName; // Save only the image name
} else {
$input[$imageField] = $updatehomeitems->$imageField;
}
}
$updatehomeitems->update($input);
return redirect()->route('admin.home.index')->with('success', 'Form Records updated successfully');
}
我想要一个功能,比如以前上传的图像应该被删除或替换为新上传的图像。 请说如何根据我的要求更改它?
试试这个
public function update(Request $request, $id)
{
$updatehomeitems = Home::find($id);
$request->validate([
'logo' => 'required|mimes:jpeg,png,jpg,gif,svg,webp|dimensions:width=160,height=40',
'image_1' => 'required|mimes:jpeg,png,jpg,gif,svg,webp|dimensions:width=960,height=700',
'image_2' => 'required|mimes:jpeg,png,jpg,gif,svg,webp|dimensions:width=960,height=700',
'image_3' => 'required|mimes:jpeg,png,jpg,gif,svg,webp|dimensions:width=1000,height=1000',
'description' => 'required',
], [
// Validation messages...
]);
$input = $request->all();
// Handle logo
if ($request->hasFile('logo')) {
$destinationPath = 'assets/images/uploads/home/';
$imageName = date('YmdHis') . $request->file('logo')->getClientOriginalExtension();
$request->file('logo')->move(public_path($destinationPath), $imageName);
// Delete old logo
if ($updatehomeitems->logo && file_exists(public_path($updatehomeitems->logo))) {
unlink(public_path($updatehomeitems->logo));
}
$input['logo'] = $imageName; // Save only the image name
} else {
$input['logo'] = $updatehomeitems->logo;
}
// Handle image_1, image_2, image_3
for ($i = 1; $i <= 3; $i++) {
$imageField = 'image_' . $i;
if ($request->hasFile($imageField)) {
$destinationPath = 'assets/images/uploads/home/';
$imageName = date('YmdHis') . "_{$imageField}." . $request->file($imageField)->getClientOriginalExtension();
$request->file($imageField)->move(public_path($destinationPath), $imageName);
// Delete old image
if ($updatehomeitems->$imageField && file_exists(public_path($updatehomeitems->$imageField))) {
unlink(public_path($updatehomeitems->$imageField));
}
$input[$imageField] = $imageName; // Save only the image name
} else {
$input[$imageField] = $updatehomeitems->$imageField;
}
}
$updatehomeitems->update($input);
return redirect()->route('admin.home.index')->with('success', 'Form Records updated successfully');
}