The rewritten title is: Remove elements one by one from an array using Laravel Ajax
P粉356361722
2023-08-28 14:20:24
<p>I'm trying to remove each element from an array that is displayed as an image via Append. </p>
<p><strong>Blade: </strong></p>
<pre class="brush:php;toolbar:false;"><h4>Menu image</h4>
<div class="col-md-12" id="cardImages"></div></pre>
<p><strong><code>Ajax function</code></strong></p>
<pre class="brush:php;toolbar:false;">$(document).on('click', '.edit', function(){
$("#cardImages").empty();
var id = $(this).attr("id");
$('#form_output').html('');
$.ajax({
url: "{{route('restaurantOffers.fetchdata')}}",
method: 'get',
data: {id:id},
dataType: 'json',
success:function(data)
{
$('#contract_id').val(id);
$('#editContract').modal('show');
$('#action').val('Speichern');
$('.modal-title').text('data update');
$('#button_action').val('update');
var cardUp = data.cardUpload;
$.each(cardUp, function(index,value) {
$('#cardImages').append('<a href="{{ url('/storage/offerPic/')}}' '/' value '" download ><img src=" {{ url('/storage/offerPic/')}}' '/' value '" alt=""></a><a class="btn btn-danger" target=" ;_blank" rel="nofollow noreferrer" href="" method="post">X</a>');
});
}
})
});</pre>
<p><strong>Controller: </strong></p>
<pre class="brush:php;toolbar:false;">function fetchdata(Request $request)
{
$id = $request->input('id');
$data = RestaurantOffer::find($id);
$output = array(
'cardUpload' => json_decode($data->cardUpload),
);
echo json_encode($output);
}</pre>
<p>cardUpload column records are stored as arrays, for example (<code>["image1.png","image2.png"]</code>). </p>
<p>The images are displaying fine in the cardImages div, but I want to create a function to remove these images one by one. </p>
<p>The question is how to delete array elements one by one. </p>
<p>PS: These images are displayed after the edit button. So I have to create delete or publish method inside get method. </p>
<p>Thanks in advance for your help</p>
To delete specific images, you should identify a specific id for each image:
([1=>"image1.png",2=>"image2.png"]).
Delete button:
Please don't suggest edits, if you suggest I can't edit it, I'll have to post another answer.