Delete all Photos within a Project when you Delete the Project

Posted 1 year ago by almost_pitt

Hey guys,

I've built something similar to Project Flyer. I would like to be able to delete all of the images that I have uploaded to one flyer whenever I delete that flyer. (In my case, these flyers are rooms.)

In my RoomsController, I have the following function for deleting a room.

    public function destroy($id)
    {
        Room::destroy($id);

        Session::flash('success', 'Room deleted successfully.');

        return redirect()->back();
    }

This deletes the room, but it doesn't delete any of the photos associated with that room.

In my create photos table, I call in the "room_id" as well so that I can link the two. Therefore, my idea was the following:

    public function destroy($id)
    {
        Room::destroy($id);

        $photos = Photo::all()->where('room_id', $id);
        $photos->destroy();

        Session::flash('success', 'Room deleted successfully.');

        return redirect()->back();
    }

However, when I run this code, I always get the error message:

1/1) BadMethodCallException Method destroy does not exist.

Do you guys have any idea how I could do this?

When I call

dd(Photo::all()->where('room_id', $id));

Then, it gives me the correct list of images associated with the room. So, that works. It's just the deleting part that's not quite there yet.

Also, here is my web.php file. I'm not sure if it'll help.

  Route::delete('rooms/delete/{slug}', [
    'uses' => '[email protected]',
    'as' => 'rooms.destroy'
    ]);
  Route::delete('photos/{id}', '[email protected]');

Thanks for any and all suggestions, ideas, and input!! :)

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.