0

I've saved images of posts in different sub-folders inside public/images like featured_image (public/images/featured_image), banner(public/images/banner). I want to delete images(featured_image and banner) related to post when banner is deleted.

This is my setup in filesystem.php

'local' => [
  'driver' => 'local',
  'root' => public_path('images/'),
],

I'm trying this to delete posts and images related to this:

public function destroy($id)
{
    //
    $tour = Tour::find($id);
    $tour->country()->detach();
    Storage::delete('featured_image/$tour->featured_image');
    Storage::delete('banner/$tour->banner');
    $tour->delete();

    Session::flash('success', 'The tour is sucessfully deleted.');
    return redirect()->route('tours.index');
}

The post gets deleted successfully but the images are not deleted from the folder. Please suggest the possible solution.

prateekkathal
  • 3,482
  • 1
  • 20
  • 40
Zachary Dale
  • 739
  • 4
  • 11
  • 30
  • Could be typo. The variables `$tour` in the `delete` function calls is not being interpreted as variables: http://stackoverflow.com/a/3446245/534862 – Lionel Chan Dec 12 '16 at 07:36
  • If i do `Storage::delete('featured_image'/$tour->featured_image); Storage::delete('banner'/$tour->banner);` I get error cannot be divided by zero. – Zachary Dale Dec 12 '16 at 07:41
  • "Division by zero" because in this way you are doing a division on two strings! Strings are converted into 0 when it contains no numbers in it – Lionel Chan Dec 12 '16 at 10:03

2 Answers2

1

It should be

Storage::delete('images/featured_image/' . $tour->featured_image);
Storage::delete('images/banner/' . $tour->banner);

If that doesn't work... you can also do.

$images = [
  'featured_image' => public_path('images/featured_image/' . $tour->featured_image),
  'banner_image' => public_path('images/banner/' . $tour->banner),
];

foreach($images as $image) {
  if(file_exists($image)) {
    unlink($image);
  }
}
prateekkathal
  • 3,482
  • 1
  • 20
  • 40
  • Non of these above worked. First one returned error of unlink problem, second one gave error of unexpected '.' in line `'featured_image' => public_path('featured_image/' . $tour->featured_image);` I got it working by using File:delete() method. – Zachary Dale Dec 12 '16 at 08:32
  • @ZacharyDale The error was not with '.', but with ';' (semi-colon). Edited my code. I am damn sure this should work though. Also, I noticed I had given wrong path to the file. Update my answer. – prateekkathal Dec 12 '16 at 09:40
0

I got this working by using File:delete(); method.

public function destroy($id)
{

    $tour = Tour::find($id);
    $tour->country()->detach();
    File::delete(public_path('/images/featured_image/' . $tour->featured_image));
    File::delete(public_path('/images/banner/' . $tour->banner));
    $tour->delete();

    Session::flash('success', 'The tour is sucessfully deleted.');
    return redirect()->route('tours.index');
}
prateekkathal
  • 3,482
  • 1
  • 20
  • 40
Zachary Dale
  • 739
  • 4
  • 11
  • 30