It seems like the user is having an issue with the image editing section of their application not updating as expected. From the provided code, the update method in the SliderController is responsible for handling the update of the slider, including the image.
Here's a revised version of the update method that ensures the image is updated correctly when a new one is uploaded:
public function update(Request $request, $id)
{
$slider = Slider::findOrFail($id);
// Check if a new image was uploaded
if ($request->hasFile('gambar')) {
// Delete the old image from storage
if ($slider->gambar) {
Storage::delete($slider->gambar);
}
// Store the new image and get the path
$gambar = $request->file('gambar')->store('images/slider');
} else {
// Keep the old image path if no new image was uploaded
$gambar = $slider->gambar;
}
// Update the slider with new data
$slider->update([
'gambar' => $gambar,
'judul1' => $request->judul1,
'judul' => $request->judul,
'text' => $request->text,
'button1' => $request->button1,
'button' => $request->button,
]);
// Prepare the success message
$message = $request->hasFile('gambar') ? 'Slider berhasil diupdate dengan gambar baru' : 'Slider berhasil diupdate tanpa mengubah gambar';
// Redirect with a success message
return redirect()->route('slider.index')->with('success', $message);
}
Make sure that the form used to submit the update request includes the enctype="multipart/form-data" attribute to allow file uploads. Also, ensure that the form method is set to POST and includes a hidden _method field with the value PUT or PATCH to spoof the HTTP method since HTML forms do not support PUT or PATCH natively.
Here's an example of how the form tag should look:
<form action="{{ route('slider.update', $slider->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<!-- form fields -->
</form>
This should resolve the issue with the image editing section not updating as expected. If the problem persists, it may be necessary to debug further by checking the request data, ensuring the file upload is successful, and verifying that the image path is correctly stored in the database.