Skip to content
Advertisement

How can I delete a single image from a post with multiple images in Laravel 7

Using Laravel 7, I can upload, add and delete all images from a task (post if it were a blog) but I am unclear on how to delete a single image from a task view (single post view). I am using resource controller for tasks but I assume I will need a different function. I created one in the same TasksController and about the resource controller, I created a delete route for it. My application complains that I am trying to get a property ‘task_name’ of non object when I click the single image delete button. I am not sure how to go about this or if I need to create an Image Controller (of which I am not using at the moment). I will show what I am attempting and maybe, you can tell me if I am off base or share some recommendations. So, to start off with, here is my web.php:

JavaScript

My TasksController showing both my destroy method as well as my newly created delete method:

JavaScript

In my show.blade.php which would act as a single post view:

JavaScript

In my Image.php Model my relationships are as follows:

JavaScript

and in my Task.php Model:

JavaScript

If I missed anything, please let me know so I can modify my question. I really appreciate the help on this as I am a week behind. Thank you in advance.

Advertisement

Answer

You need to pass the particular image_id to delete it. I have also made some changes for cleaner code:

JavaScript

Note that delete button now takes you to …/delete-image/{image_id}

then on controller find the id and delete it.

Update your route file to accept id as Variable

JavaScript

then on controller variable can be accessed as

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement