Laravel更新后删除旧图像
我希望在用户更新旧图像时将其删除。此当前函数无法按我所希望的方式工作。我将如何更改它以使其工作Laravel更新后删除旧图像,laravel,Laravel,我希望在用户更新旧图像时将其删除。此当前函数无法按我所希望的方式工作。我将如何更改它以使其工作 public function update($id) { $profile = Profile::findOrFail($id); $data = request()->validate([ 'title' => 'required|max:255',
public function update($id)
{
$profile = Profile::findOrFail($id);
$data = request()->validate([
'title' => 'required|max:255',
'image' => ''
]);
if ($profile->image) {
if (Storage::exists("storage/{$profile->image}")) {
Storage::delete("storage/{$profile->image}");
}
}
if (request('image')) {
$imagePath = request('image')->store('profile', 'public');
$image = Image::make(public_path("storage/{$imagePath}"))->orientate()->fit(1000, 1000); //Intervention Image Package
$imageArray = ['image' => $imagePath];
$image->save();
}
// $profile->image = $request->image; //Lägg till senare
$profile->update(array_merge(
$data,
$imageArray ?? [],
));
return redirect("/profile/{$profile->user_id}");
}
}
首先,您必须获取旧图像并将其从服务器中删除
if (file_exists(('./images/partners/' . $partner->image_path))) {
unlink(('./images/partners/' . $partner->image_path));
}
之后,您可以上载新图像并更新数据库中的记录好的,在不使用存储功能时,它似乎可以工作,谢谢!