I don't really understand why you send an ajax request in this case.
If you use blade and simply want to redirect, why not simply post the form to the controller/update method.
Even with ajax, you will never get your flashy message....
and your update method should return a response...something like
return response()->json(['status'=>false ]);
Then you should check the response or the failed return, and perform an action depending on this return response
Javascript to Laravel controller
Hello
I have a view for edit (update) items, this item contains attachments (files).
My objective is when the user will delete the file, he clicks on a button, this button generates a javascript array listing the files to delete. And when the user sends the form, the file is deleted and the inputs of item are updated.
For this, I send an ajax method, the problem is the ajax method just send my array. But I want to send the array and my inputs to update like that the controller can delete the file or/and update inputs, all at the same time.
Actually, my code deletes my attachment, but it's return an error because the inputs are null. When I comment the code for update an item, the view is not returned.
My ajax method (javascript)
$('form.ajax').on('submit', function(event){
//event.preventDefault();
var formData = {
_token : $('meta[name=csrf-token]').attr('content'),
_method : 'PUT',
attachments_to_delete : attachments_to_delete
}
$.ajax({
method : "POST",
// url : $(this).attr('action') + '/store',
url : $(this).attr('action'),
data : formData,
cache : false,
success : function(data) {
console.log(data);
}
})
return false;
});
My controller
public function update(Request $request, $id)
{
//dd($request);
foreach ($request->attachments_to_delete as $id_attachment) {
$attachment = ClientAttachment::find($id_attachment);
$attachment->delete();
}
$client = Client::findOrFail($id);
$client->update([
'name' => $request->name,
'firstname' => $request->firstname,
'date_of_birth' => $request->date_of_birth,
'email' => $request->email,
'telephone' => $request->telephone,
'mobile' => $request->mobile,
'pro_mobile' => $request->pro_mobile,
'street' => $request->street,
'locality' => $request->locality,
'country' => $request->country,
'note' => $request->note,
'active' => $request->active ? 1 : 0,
]);
Flashy::message('Client modifié avec succès!', route('client.show', $client));
return Redirect::route('client.show', $client);
}
Thanks for answer
Solved: To solve this problem, I created 2 routes.
The first one to delete my file and the second to post my form data.
So with with my javascript, I do 2 ajax request, one fort delete my file and the second for send data.
Thanks for all
Please or to participate in this conversation.