How to call my UPDATE route through the action of the form in laravel?

1

I'm trying to call the update path using the form's action and it does not send me any errors, but even then in the database my registry does not update. I'm using laravel's Route :: resource.

This is my form:

 //Aqui quiero llamar la funcion
<form method="POST" action="http://localhost:8000/conceptos" accept-charset="UTF-8" enctype="multipart/form-data">

      <input type="hidden" name="_token" value="<?php echo csrf_token();?>">
      <input type="hidden" name="_method" value="PUT">

        <label class="col-md-4 control-label">idconcepto</label>
          <input type="number" class="form-control" name="idconcepto" value="2" >

          <label class="col-md-4 control-label">idgrupo</label>
          <input type="number" class="form-control" name="idgrupo" >

          <label class="col-md-4 control-label">titulo</label>
          <input type="" class="form-control" name="titulo" >

          <label class="col-md-4 control-label">sustento</label>
          <input type="" class="form-control" name="sustento" >

          <label class="col-md-4 control-label">periodicidad</label>
          <input type="" class="form-control" name="periodicidad" >

          <button type="submit" class="btn btn-primary">Enviar</button>

      </form>

My route

 Route::resource('/conceptos', 'Conceptos',['except' => ['edit','create']]);

My controller

public function update(Request $request, $id)
{
    $validator = Validator::make($request->all(),[
        'titulo' => 'required|max:150',
        'sustento' => 'required|max:45',
        'periodicidad' => 'required|max:20'
    ]);

    if ($validator->fails()){
        return response()->json([
            'error' => $validator->messages()
        ],400);
    }

    if($this->checkName($request->nombre,$id)){
        return response()->json([
            'message' => 'Ya existe un concepto con el mismo nombre'],400);
    }

    $concepto = Concepto::where('estatus',1)->find($id);
    $concepto->fill($request->all());
    $concepto->save();

    return $concepto;
}
    
asked by Blidge 17.10.2017 в 18:23
source

2 answers

1

The first thing is that Laravel uses the verb PUT or PATCH for the updates, so it would be the first modification, in addition, you are not passing the id (or key) of the concept you want to update, ideally using Route Model Binding.

If you want to continue using the URL and not the name of the route, this should work:

<form method="PUT" action="http://localhost:8000/conceptos/<?php echo $concepto->id ?>" accept-charset="UTF-8" enctype="multipart/form-data">

A best practice is to use the name of the route, so that it is independent of the url, by means of the helper route ():

<form method="PUT" action="{{ route('conceptos.update', ['id' => $concepto->id) }}" accept-charset="UTF-8" enctype="multipart/form-data">

Bearing in mind that I have not tested this code, you may have to make some modifications to your update driver's method, assuming you use Route Model Binding , which I highly recommend too. Please review in detail the Laravel documentation.

    
answered by 17.10.2017 / 18:38
source
0

Your problem will be in this line

  $concepto = Concepto::where('estatus',1)->find($id);

Why the concept you are looking for may not exist with status 1

so I recommend that you do it like that

$concepto = Concepto::where('estatus',1)->find($id);
if(is_null($concepto)){
  //No se puede acutualizar no cumple el filtro
  return redirect()->back();
}
$concepto->fill($request->all());
$concepto->save();
  

However, I recommend that you print the model before doing the fill to see if it brings the correct model, thus remaining

$concepto = Concepto::where('estatus',1)->find($id);
dd($concepto); //esto te imprimira en pantalla el modelo
    
answered by 17.10.2017 в 18:59