mardi 3 mai 2016

I can't make PUT or PATCH - Laravel 5.2

I want to make a PUT and PATCH and this is my function update in my controller:

public function update(Request $request, $id_funcionario) {
   try{
    $method = $request->method(); 
    $statusCode = 200;
    $funcionario = funcionario::find($id_funcionario);

    $nombre = $request->input('nombre');
    $apellido = $request->input('apellido');
    $ci = $request->input('ci');
    $rango = $request->input('rango');
    $direccion = $request->input('direccion');
    $telefono = $request->input('telefono');

    if($request->isMethod('PATCH'))
    {
        $bandera = false;
        if ($nombre) {
            $funcionario->nombre = $nombre;
            $bandera = true;
        }

        if ($apellido) {
            $funcionario->apellido = $apellido;
            $bandera = true;
        }

        if ($ci) {
            $funcionario->ci = $ci;
            $bandera = true;
        }

        if ($rango) {
            $funcionario->rango = $rango;
            $bandera = true;
        }

        if ($direccion) {
            $funcionario->direccion = $direccion;
            $bandera = true;
        }

        if ($telefono) {
            $funcionario->telefono = $telefono;
            $bandera = true;
        }

        if ($bandera) {
           $funcionario->save();

            return \Response::json(array(
            'error' => false,
             'message' => 'Funcionario actualizado'), 200
            );
        }

  }

    $funcionario->nombre = $request->nombre;
    $funcionario->apellido = $request->apellido;
    $funcionario->ci = $request->ci;
    $funcionario->rango = $request->rango;
    $funcionario->direccion = $request->direccion;
    $funcionario->telefono = $request->telefono;
    $funcionario->save();

    } catch (Exception $ex) {
       $statusCode = 404;
    } finally{
       return \Response::json(array('funcionarios' => $funcionario->toArray(), $statusCode));

    }
}

And nothing happens. If I make a PUT, all the values reset to null and if I make a PATCH, nothing happens. But the interesting thing is that doesn't even recognize the PATCH method when I do it in Postman. What is wrong with my code? what did I miss? Thank you. I'm new in all this.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1TJmccW
via IFTTT

Aucun commentaire:

Enregistrer un commentaire