error in datatable laravel actions

1

I am making a table with actions of habilitar and deshabilitar when clicking on the buttons the url sends me with its respective id but the function does not call me in the controller.

Route::get('/admin/votos/all/{id}/deshabiliar','NombrevotosController@deshabiliar');
Route::get('/admin/votos/all/{id}/habilitar','NombrevotosController@habilitar');

I do not know if I'll be misplacing the routes

 @foreach ($proyectos as $proyecto)
<tr>

   <td>{{$proyecto->id}}</td>
  <td>{{$proyecto->nombre}}</td>
  <td>
    @if ($proyecto->acciones == 'Habilitado')
    @if ($proyecto->acciones == 'Habilitado')
    <form action="{{url('/admin/votos/all/'.$proyecto->id.'/deshabilitar')}}" method="post">
    <input type="submit" class="btn btn-primary" name="id" value="deshabilitar">
    </form>
    @else  
      <form action="{{url('/admin/votos/all/'.$proyecto->id.'/habilitar')}}" method="post">
     <input  class="btn btn-danger" type="submit" name="id" value="habilitar">
       </form>
     @endif
  </td>
</tr>
  @endforeach

    
asked by Ramon Albares 23.10.2018 в 17:18
source

1 answer

1

As you can see in this line:

<form action="{{url('/admin/votos/all/'.$proyecto->id.'/deshabilitar')}}" method="post">

Your form is invoking the POST method in the specified endpoint. However, in your routes you have them defined as type GET :

Route::get('/admin/votos/all/{id}/deshabiliar','NombrevotosController@deshabiliar');
Route::get('/admin/votos/all/{id}/habilitar','NombrevotosController@habilitar');

Try changing them to POST :

Route::post('/admin/votos/all/{id}/deshabiliar','NombrevotosController@deshabiliar');
Route::post('/admin/votos/all/{id}/habilitar','NombrevotosController@habilitar');

Observation

This has nothing to do with your question, but I imagine that both methods are used to change the state of an object between enabled / disabled.

These actions could be done with a single route that is type " changeState ", here you detect if it is enabled and you change the status and vice versa.

It's just a tip to optimize the code;)

    
answered by 23.10.2018 / 18:01
source