Laravel 5.6后台面板ProductController资源更新方法问题求助
Fixing Image Update Logic in Laravel 5.6's Product (Urun) Update Method
Hey there! Let's get that update method sorted for your admin panel. Since you already have the image upload working in your store method, we can adapt that logic to handle product updates—including cases where the user might upload a new image or keep the existing one.
Step-by-Step Solution
Here's how to structure your update method properly:
- Retrieve the existing product: Fetch the
Uruninstance you want to update first. - Handle new image upload (if provided): If a new image is uploaded, delete the old one to clean up storage, then process and save the new image.
- Update product fields: Sync all other form data, and only update the
filenameif a new image was uploaded. - Save the changes: Persist the updated product to the database.
Complete Update Method Code
public function update(Request $request, $id) { // Validate your request first (don't skip this!) $request->validate([ 'k_id' => 'required', 'ad' => 'required', 'filename' => 'image|mimes:jpeg,png,jpg,gif|max:2048', // Adjust rules as per your needs ]); // Get the existing product $urun = Urun::findOrFail($id); // Handle image upload if a new file is provided if ($request->hasFile('filename')) { // Delete the old image from storage (if it exists) if ($urun->filename && file_exists(public_path('/images/urunler/' . $urun->filename))) { unlink(public_path('/images/urunler/' . $urun->filename)); } // Process the new image (reusing your store method logic) $file = $request->file('filename'); $name = $file->getClientOriginalName(); // Resize and save the image $image_resize = Image::make($file->getRealPath()); $image_resize->resize(590, 590); $image_resize->save(public_path('/images/urunler/' . $name)); // Update the filename in the product instance $urun->filename = $name; } // Update other product fields $urun->k_id = $request->get('k_id'); $urun->ad = $request->get('ad'); // Add all your remaining fields here... // Save the updated product $urun->save(); // Redirect with success message return redirect()->route('urun.index')->with('success', 'Product updated successfully!'); }
Key Notes to Remember
- Validation: Always validate image inputs to ensure only valid types/sizes are uploaded.
- Clean Up Old Images: Deleting the old image prevents storage bloat. We added a check to confirm the file exists before deletion to avoid errors.
- Form Enctype: Make sure your update form includes
enctype="multipart/form-data"so file uploads work correctly. - Intervention Image: Double-check that you have the Intervention Image package installed and configured (since you're using
Image::make()). If not, runcomposer require intervention/imageand follow Laravel's setup steps.
Optional: Avoid Duplicate Filenames
If multiple users might upload images with the same name, generate a unique filename to prevent overwrites:
$name = time() . '-' . $file->getClientOriginalName(); // Adds a timestamp to the filename
That should cover all the bases for updating your product's image. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Oğuz Korkmaz
相关产品推荐
相关产品推荐

