Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mcardosob's avatar

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

0 likes
5 replies
KNietzsche's avatar

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

1 like
Cronix's avatar

You are not sending name, firstname, date_of_birth, etc, in the ajax request, so when you $client->update([data]), you're setting null values. You're only sending _token, _method and attachments_to_delete.

You also can't send a redirect as a reply to an ajax call. It will have no effect. You could send a url back as part of the response, and then use window.location.href = theNewUrl to redirect once you receive the response. But, as @KNietzsche points out, there's no benefit to doing this with ajax if you're going to just redirect anyway.

2 likes
mcardosob's avatar

@KNietzsche thank you for your answer. I do an ajax method because I want to collect the attachments to delete before sending my form. Like this, the user can cancel the modifications, because the attachments doesn't are deleted.

Maybe you have a better methodology to do this..? Or how I can do this ?

@Cronix thank you for your answer. I explain why I want to use ajax.

KNietzsche's avatar

if your item has attachements....then you should have defined somewhere a relationship in your model Item .... hasManyso, if you want to delete the item, you can easily find your attachements in your controller with Item::find($id)->attachements()->get() Also you could add a softDelete in your attachement, and only delete the items with your ajax..but as I don't understand all your flow and logic, I prefer not to go deeper.... The route you call to perform this task is confusing as it calls a method that seems to not do what it was created for...so maybe begin to create a new API route or other route and method to separare the tasks....

mcardosob's avatar
mcardosob
OP
Best Answer
Level 1

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

1 like

Please or to participate in this conversation.