P粉0025726902023-08-29 11:16:53
要刪除特定的圖像,您應該為每個圖像確定一個特定的id:
([1=>"image1.png",2=>"image2.png"]).
function delete(Request $request) { $id = $request->input('id'); $imageIdToDelete=$request->input('image_index') ; $data = RestaurantOffer::find($id); $images=json_decode($data->cardUpload,true) ; if(isset($images[$imageIdToDelete])){ unset($images[$imageIdToDelete]) ; } $data->update([ 'cardUpload'=>json_encode($images) ]) ; }
刪除按鈕:
$.each(cardUp, function(index,value) { $('#cardImages').append('<a href="{{ url('/storage/offerPic/')}}'+'/' + value +'" download > <img src="{{ url('/storage/offerPic/')}}'+'/' + value +'" alt=""></a> <button class="btn btn-danger delete" data-index=index >delete</button>'); }); $('delete').on('click',function(){ var image_index= $(this).attr('data-index') }) $.ajax({ url: "{{route('restaurantOffers.delete')}}", method: 'get', data: {image_index:image_index}, dataType: 'json', })
請不要建議編輯,如果您建議我無法編輯它,必鬚髮布另一個答案。