我可以存储和删除所有多个图像。但是更新和删除旧图像时我仍然有问题。更新新映像时,我的代码仍然无法删除oldimage。
我的更新控制器,我使用handlerequest方法来存储图像,其中oldImage是数据库中的变量,它将与新图像进行比较。如果不相同,则删除旧图像。
public function update(Requests\UpdatePostRequest $request, $id)
{
$post = Post::findOrFail($id);
$oldImage = $post->image;
$data = $this->handleRequest($request);
$post->update($data);
if($oldImage != $post->image)
{
foreach (json_decode($post->image, true) as $oldImage) {
$this->removeImage($oldImage);
}
}
if($request->submitbutton =='Publish')
{
Alert::success('Your post was updated successfully')->persistent('Close');
return redirect(route('admins-blogpost.index'));
}
if($request->submitbutton =='Save Draft')
{
Alert::success('Your draft was updated successfully')->persistent('Close');
return redirect(route('admins-blogpost.index'));
}
}
我的handlerequest方法用于存储多个图像,该方法正在使用store方法。所以这个问题不在这里。
private function handleRequest($request)
{
$data = $request->all();
if($request->hasFile('image'))
{
$images = $request->file('image');
foreach($images as $key=>$image){
$fileName = $image->getClientOriginalName();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
if($successUploaded)
{
$width = config('cms_blog.image.thumbnail.width');
$height = config('cms_blog.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make($destination . '/' . $fileName)
->resize($width,$height)
->save($destination . '/' . $thumbnail);
$datax[] = $fileName;
}
$data['image'] = json_encode($datax);
}
}
return $data;
}
如何解决我的问题,以便在旧图像与新图像不相同时删除旧图像?
慕码人2483693
开满天机