How to receive text, files by the put method in laravel 5.6 (api restFULL)

0

I am developing APIS with laravel 5.6, I have a driver that receives images and text, by post everything works normally for me here I leave the code js

JS

 function registraPET(){

                var datos = new FormData();
                    jQuery.each(jQuery('.fileExcel')[0].files, function(i, file) {
                        datos.append('pet', file);
                    });
                    datos.append('fechaRecepcion','2018-10-10');
                    datos.append('idusuregistropet',1);
                    datos.append('codpetrecibida','asds');
                    datos.append('idcliente',1);
                    $.ajax({
                        url: 'http://127.0.0.1:8000/lineatiempopet',
                        type: 'post',
                        cache: false,
                        contentType: false,
                        processData: false,
                        data:datos,
                        success: function(response){
                            console.log(response)
                        },
                        error: function(jqXHR,status, errorThrown ){
                            console.log(jqXHR)
                        }
                    });
        }

PHP (LARAVEL 5.6) STORE FUNCTION / POST METHOD

public function store(Request $request)
    {
       /* $request->validate([
            'fechaRecepcion'=>'required|date',
            'idusuregistropet'=>'required|int',
            'codpetrecibida'=>'required|max:20',
            'fechavisitatecnica'=>'nullable|date',
            'urlpetrecibida'=>'nullable',
            'idusuvistatecnica'=>'nullable|int',
            'idcliente'=>'required|int',
            'observacion'=>'nullable|string|max:300',
            'idcot'=>'nullable|int',
            'fechaEjecucion'=>'nullable|date',
            'urlactaconformidad'=>'nullable|string',
            'urlordenCompra'=>'nullable|string',
            'fechareprogramacion'=>'nullable|date',
            'nrofactura'=>'nullable|string',
            'estado'=>'nullable|string|max1',
        ]);
        if($request->hasFile('pet')){
            $pet = $request->file('pet')->store('/public/documentacionPets/pets');
            $newLienaPET = new LineaTiempoPet();
            $newLienaPET->fechaRecepcion = $request->fechaRecepcion;
            $newLienaPET->idusuregistropet = $request->idusuregistropet;
            $newLienaPET->codpetrecibida = $request->codpetrecibida;
            $newLienaPET->fechavisitatecnica = $request->fechavisitatecnica;
            $newLienaPET->urlpetrecibida = Storage::url($pet);
            $newLienaPET->idusuvistatecnica = $request->idusuvistatecnica;
            $newLienaPET->idcliente = $request->idcliente;
            $newLienaPET->observacion = $request->observacion;
            $newLienaPET->idcot = $request->idcot;
            $newLienaPET->fechaEjecucion = $request->fechaEjecucion;
            $newLienaPET->urlactaconformidad = $request->urlactaconformidad;
            $newLienaPET->urlordenCompra = $request->urlordenCompra;
            $newLienaPET->fechareprogramacion = $request->fechareprogramacion;
            $newLienaPET->nrofactura = $request->nrofactura;
            $newLienaPET->estado = $request->estado;
            $newLienaPET->save();
        }else{
            return response()->json(['data'=>'Debe agregar una  PET','codigo'=>'400'],400);      
        }*/
        $linaTiempoPet = $request -> all(); 


        return response()->json(['data'=>$linaTiempoPet,'codigo'=>'200'],200);

    }

The problem is the function update / method put no data arrives

JS

Function updatePET(){
            var datos = new FormData();
            jQuery.each(jQuery('.fileExcel')[0].files, function(i, file) {
                        datos.append('pet', file);
                    });
                    datos.append('fechaRecepcion','2018-10-10');
                    datos.append('idusuregistropet',1);
                    datos.append('codpetrecibida','asds');
                    datos.append('idcliente',1);
                    $.ajax({
                        url: 'http://127.0.0.1:8000/lineatiempopet/1',
                        type: 'put',
                        cache: false,
                        contentType:false,
                        processData: false,
                        data:datos,
                        success: function(response){
                            console.log(response)
                        },
                        error: function(jqXHR,status, errorThrown ){
                            console.log(jqXHR)
                        }
                    });

        }

PHP (LARAVEL 5.6) UPDATE FUNCTION / PUT METHOD

  public function update(Request $request, LineaTiempoPet $lineatiempopet)
    {   
        //$pet = $request->file('pet')->store('/public/documentacionPets/pets');
        //$lineatiempopet->urlpetrecibida = $pet;
        $as = $request -> all();
        return response()->json(['data'=>$as,'codigo'=>'200'],200);
    }

As you can see in the controller I am returning everything that the view sends me, this I do to buy that as the data arrives, in the store function everything arrives normal the file is saved and everything goes fine but in the function update no data arrives THANK YOU IN ADVANCE TO THOSE WHO CAN HELP ME

    
asked by Frank Campos Vilchez 12.12.2018 в 19:03
source

1 answer

0

I think that to do that you need to add 2 fields in your form "{{method_field ('PUT')}}" (the path is set to PUT) and a "{{csrf_field ()}}" (which does not is nothing more than the token if you use it) and the method for ajax would be "POST" sending those two values to your path declared as "resource" should go perfect, I hope you make me understand and serve as a reference for my comment. Greetings.

    
answered by 12.12.2018 в 20:35