I am attempting to delete an item within a foreach loop. In this loop, there are several images within $property. For each image ($propimg), I want to delete each image using it's id. However the link doesn't work. How do I get it to delete the individual image?
@foreach($property->images as $propimg)
<li>{{ $propimg->id }}<br/>{{ $propimg->image_url }}</li>
<a href="/property_gallery/{{ $propimg->id }}" data-method="delete">Delete</a>
@endforeach
You need to perform an Ajax request, please try this:
$("[data-method='delete']").click(function(event) {
event.preventDefault();
$.ajax({
type: "DELETE",
url: $(this).prop("href")
}).always(function () {
location.reload();
});
});
If you get 500 HTTP error, due to CSRF token mismatch, please add this:
$.ajaxSetup({
headers: {
"X-XSRF-TOKEN": document.cookie.match('(^|; )XSRF-TOKEN=([^;]*)')[2]
}
});