$('#modal-save').on('click', function(event){
$.ajax({
method: 'POST',
url: urlEdit,
data: {
body: $('#post-body').val(),
deletePhoto: deletePhoto,
postId: postId,
_token: token
}
})
.done(function(msg){
$(postBodyElement).text(msg['new_body']);
location.reload();
$('#edit-modal').modal('hide');
});
});
What I am trying to do is edit a post. What happens is, an edit modals opens and previous data and photos are displayed to the user. If the user clicks on the delete button all the photos to that post are deleted.He can edit the text that will be updated in the database. Right now I am sending the data required to do this using ajax. Now I want to insert new photos to the that post. I have a multiple input tag in my html.
<input id="post-input" name="photos[]" class="post-input" type="file multiple accept='image/*,video/*' />
and my controller function looks something like this
public function postEditPost(Request $request)
{
$this->validate($request, [
'body' => 'required'
]);
$post = Post::find($request['postId']);
if (Auth::user() != $post->user){
return redirect()->back();
}
$photos = Photos::where('post_id', $request['postId'])->get();
$deletePhoto = $request['deletePhoto'];
if($deletePhoto == 0){
foreach($photos as $photo){
$photo->delete();
}
}
$post->body = $request['body'];
$post->update();
return response()->json(['new_body' => $post->body],200);
}